having trouble with a regexp

Hello,
I’m a beginner, but I’m pretty intuitive, and have some experience with other languages. I have a regular expression I need to evaluate as part of a preg_replace statement. I’m trying to add to some phpBB code for my own purposes. Since this is a PHP problem and not a phpBB problem, I thought it would be more appropriate to ask about it here.

I want to match only if there is a newline, space, or tab in front of the pattern.
I want to match only if there is a newline, space, tab, period, question mark, exclamation point, dash, s, or y after the pattern.

Here’s what I have so far (‘AC’ is the pattern to find):

preg_replace("/[s]AC[.-!?sys]/", $replacement, $subject)

I guess you could say this was hacked together from a number of different tutorials. :-? As I understand it, the s will cover the newline-space-tab? The period and question mark need to be escaped? The slashes surrounding the pattern are required?

It looks like it would work. But I get an error: “Warning: preg_replace() [function.preg-replace]: Compilation failed: range out of order in character class at offset 10 in…”, and Googling tells me this is a regexp failure. Not too surprising, considering that my code doesn’t look anything like the regexps I have seen in PHP elsewhere.

Any help on this would be greatly appreciated.

Howdy,
I ready like this webpage for testing php regular expressions:
http://www.quanetic.com/regex.php

You were missing the |(ors) between your values in the []. Perl doesn’t require the pipes, but php does.
/s+AC(.|-|!|?|s|y|s)/
so your code would be:

preg_replace("/s+AC(.|-|!|?|s|y|s)/", $replacement, $subject)

I also got rid of the [] in front of AC, since you only have a single thing to match on your don’t need it. adding the + to /s will make it match 1 or more blank spaces ( and remove them with the replace). Another thing to consider is do you only want to match when there is one of the specific vars and not any other? ’ AC! ’ for instance? If you would like to also match on white space around the matching char you should do something like:

preg_replace("/s+ACs*(.|-|!|?|s|y|)s*/", $replacement, $subject)

the * means zero or more spaces.
Good Luck

Thanks, I’ll try that out. On the +, I don’t actually want to remove any spaces, as I am replacing acronyms in text passages. I want to make sure that the AC from ‘AC!’ and ‘ACs’ gets matched, but not from ‘ACTION!’ or ‘EXACTLY’. I’m not interested in replacing the ‘s’ or ‘!’, I just want to make sure they can be used without being ignored.

you might want to try preg_match instead of replace…then the sting won’t be altered.

Sponsor our Newsletter | Privacy Policy | Terms of Service