• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Perl - Need regular expressions replacement help..

brxndxn

Diamond Member
I need to get better at regular expressions with Perl.. I wrote a script that does 98% of what I need and screws up 2%. That screwed up 2% causes problems.

I am trying to replace a list of expressions with another list of expressions.. The easy part was to write a script that runs through the list. The hard part is getting the regular expressions to work correctly.

For example:
$currentline =~ s/ListItem1/ListItem5/gi; works fine..

But, in my list, I have a lot of items within items... like:
$currentline =~ s/ListItem1/ListItem5/gi;
$currentline =~ s/ListItem1_1/ListItem5_2/gi;

so the second line never runs because all the ListItem1_1's are changed to ListItem5_1 already

How would I tell the first line to skip 'ListItem1' if it is followed by an underscore and a number? I tried a few regular expressions tutorials and I am not quite following them.
 
The quick solution is to just reverse the lists of items before you run the expression.

The more robust solution (and the direct answer to your question) would be:

Code:
$currentline =~ s/$searchitem(?!_\d)/$replaceitem/gi;

This feature is known as "negative look-ahead". Support for it is for from universal, but has been in Perl for a long time.
 
Back
Top