Forum Moderators: coopster

Message Too Old, No Replies

regular expression for FIND/REPLACE

syntax issue with a regular expression

         

wyattea

2:40 am on Oct 25, 2007 (gmt 0)

10+ Year Member



Hello, I'm trying to do a 'find and replace' of a small piece of code, but it's not finding the closing parenthesis. Here's the code:

[($[0-9]+<font size="-2">US</font>)]

It's supposed to find this:

($57<font size="-2">US</font>)

But it's not finding the last character (closing parenthesis so only this is found:

($57<font size="-2">US</font>

I want to remove that code from 100's of pages, but it defeats the purpose if a ')' is left in it's place on the pages. Anyone know if a closing parenthesis is some kind of escaped character or special character?

Regards,

James...

[edited by: eelixduppy at 2:41 am (utc) on Oct. 25, 2007]
[edit reason] disabled smileys [/edit]

PHP_Chimp

1:18 pm on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The () are special characters in the regular expressions. So if you want to find literal ( or ) then you need to escape them i.e. \(

In the regular expression the () are used to define a captured pattern. So you may well need to use something along the lines of -
(\($[0-9]+<font size="-2">US</font>\))

Have a look at the PCRE pattern syntax [uk3.php.net] for the full description.

wyattea

1:44 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



That's the funny thing, NOTHING is ever found unless it's the code I used, with the exception of the missing ending parenthesis. In dreamweaver's find/replace, there's a checkbox to mark to indicate if it's a regex your using. Maybe that automatically escapes everything? So assuming that's the case, this works:

[($[0-9]+<font size="-2">US</font>)] (this is exactly as i put it in the search box and it finds all instances but only HIGHLIGHTS this:

($29<font size="-2">US</font>
^^^
So it misses the closing parenthesis which defeats the purposes of finding all instances to remove it.

Any thoughts?

James...

[edited by: eelixduppy at 3:46 pm (utc) on Oct. 25, 2007]
[edit reason] disabled smileys [/edit]

PHP_Chimp

6:22 pm on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The [ and ] mark the start and end of a character class. What you are looking for is not a character class. However if you introduce it as a character class by using [whatever] the regex will be looking for a single w, h, a, t, e, v or r, as that is what the character class is. Also the - within a character class (as you have -2) means a character range. So you were specifying any character from " to 2, and im sure that is not what you meant.

The $ that you have in the string, if not escaped will mean the end of the string to search. So that will need escaping.

So you can get rid of the []'s as you are not looking for a character class.

Also dont believe everything that Dreamweaver tells you...like most products if it doesnt work then the problem is not with the manual, its with the automated system you are using to generate the code.

I have tested this and it works. So hopefully it is what you are after.


$pattern = '%^(\(\$[0-9]{1,2}<font size="\-2">US</font>\))+$%';
$sub = '($57<font size="-2">US</font>)';
if (preg_match($pattern, $sub) == 1){
echo 'Working<br />';
}
else{
echo 'You suck<br />';
}

At the moment the pattern will only recognise 1 or 2 numbers after the $ sign,so if you want more then change the numbers in the {}'s.

[edited by: eelixduppy at 2:10 pm (utc) on Oct. 26, 2007]
[edit reason] disabled smileys [/edit]

wyattea

11:09 am on Oct 26, 2007 (gmt 0)

10+ Year Member



PHP_Chimp, you say you've tested it, where? I put the code in the find/replace box, and nothing is found. I copied what you wrote and made no revisions.

Regards,

James...

wyattea

11:18 am on Oct 26, 2007 (gmt 0)

10+ Year Member



PHP_Chimp:

This worked:

\(\$[0-9]+<font size="-2">US</font>\)

and it found even 3 digit references.

Thanks for the help, i appreciate it!

James...

vincevincevince

11:24 am on Oct 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First thing to remember about regular expressions is this:
Escape everything which is not a-z or 0-9, without exception, by adding a backslash

Take your text:
($54<font size="-2">US</font>)

Escape everything apart from a-z and 0-9:
\(\$54\<font\ size\=\"\-2\"\>US\<\/font\>\)

Change the variable bit into a wildcard:
\(\$[0-9]+\<font\ size\=\"\-2\"\>US\<\/font\>\)

Remember, surround it with some delimiters before testing, I prefer /:
/\(\$[0-9]+\<font\ size\=\"\-2\"\>US\<\/font\>\)/

[edited by: eelixduppy at 2:11 pm (utc) on Oct. 26, 2007]
[edit reason] disabled smileys [/edit]