Find and replace numbers between a number range

Find, replace, find in files, replace in files, regular expressions

Find and replace numbers between a number range

Postby mprlak » Sat Apr 30, 2011 3:29 am

Hi!

I want the javascript or regular expression code for following issue.

***********
find 1 to 15
And replace with
<a href="part1.html#page_1">1</a>
<a href="part1.html#page_13">13</a>
***********

Another one

********
find 16 to 29
and replace with
<a href="part2.html#page_18">18</a>
<a href="part2.html#page_28">28</a>
**********

try to understand the above mentioned issue.
Thanks in advance
mprlak
Newbie
 
Posts: 9
Joined: Sat Jan 01, 2011 2:23 am

Re: Find and replace numbers between a number range

Postby Mofi » Sat Apr 30, 2011 8:49 am

For 1 to 15 use the Perl regular expression searching for \<([1-9]|1[0-5])\> and using <a href="part1.html#page_\1">\1</a> as replace string.

For 16 to 29 the replace string is the same, but the search string is \<(1[6-9]|2[0-9])\>

And in case you want to search for 13 to 71, the search string would be \<(1[3-9]|[2-6][0-9]|7[01])\>
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Find and replace numbers between a number range

Postby mprlak » Wed May 18, 2011 11:13 pm

Thank you for your reply

and once more coding needed

find the numbers between 1-160.

please reply to this.
mprlak
Newbie
 
Posts: 9
Joined: Sat Jan 01, 2011 2:23 am

Re: Find and replace numbers between a number range

Postby Mofi » Thu May 19, 2011 1:43 am

Search string for 1 - 160 (without leading zeros) is: \<([1-9][0-9]{0,1}|1[0-5][0-9]|160)\>
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Find and replace numbers between a number range

Postby mprlak » Wed Jun 08, 2011 7:07 am

Hi!

I want one more coding for replace with Perl. Need to find 204 to 224

thanks in advance
mprlak
Newbie
 
Posts: 9
Joined: Sat Jan 01, 2011 2:23 am

Re: Find and replace numbers between a number range

Postby Mofi » Wed Jun 08, 2011 10:14 am

Come on, you should have understood already the concept for finding the appropriate expression for a number range.

The new search string is: \<(20[4-9]|21[0-9]|22[0-4])\>

\< ... found string must be at beginning of a word

(...) ... tag the found string for re-use in replace referenced by \1. Also needed for the OR expression inside the round brackets.

20[4-9] ... find the string "20" and one more character in range '4' to '9'.

| ... OR

21[0-9] ... find the string "21" and one more character in range '0' to '9'.

| ... OR

22[0-4] ... find the string "22" and one more character in range '0' to '4'.

\> .. and found string must stop at end of a word.

\< and \> make sure that not a part of a larger number like for 12185 is found.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Find/Replace/Regular Expressions