Re: Need help w/ reqexp, capture and substitue




On Oct 29, 2006, at 7:56 PM, Dennis G. Wicks wrote:



Shawn Milochik wrote:
This seems to work for me:
#!/usr/bin/perl -w
use strict;
while (<STDIN>) {
chomp;
print "Original: $_\n";
$_ =~ s/^([^@]+)(@.+&\d+&)([&]{5})/$1$2$1$3/;
print "Modified: $_\n";
}
Hope it helps.
Shawn

Thanks Shawn!

That works perfectly!

I just might get finished by Monday AM.

Now I just need to figure out how it works
so I don't beat my head against the desk so much
the next time I need to do something like this.

Regards,
Dennis

-

Dennis,

^([^@]+) = Take everything starting from the beginning of the line as long as it doesn't contain an @.
Literally: Match one or more characters if not the @ character. Thanks to "greedy matching," the fact that the next part of the regular expression begins with the @ means that you'll get every single character up to that @.

(@.+&\d+&) = From the @, take everything up until you hit an & followed by one or more digits, followed by another ampersand.
Literally: Match @, then any character (.) for one or more (+), then & then a digit (\d) one or more of them (+) then &.

([&]{5}) = Match exactly five & characters in a row.
Literally: Match exactly five & in a row.

In each case, the section is surrounded by parenthesis. These parenthesis allow you to refer to $1, $2, and $3 in the substitution. $1 is whatever is in the first set of parenthesis, and so on.

I very highly recommend "Mastering Regular Expressions" by Jeffrey Friedl. I think that's the best way to learn, and the book is very easy to follow. The third edition was just released.

Shawn


.



Relevant Pages

  • Re: Regular Expressions in C#
    ... The function name Regular Expression is: ... by an opening parenthesis. ... It is always followed by either a comma or an end-parenthesis. ... Any parameter which precedes ...
    (microsoft.public.dotnet.general)
  • Re: Problem finding last parenthetical phrase with Regular Expressions
    ... The coding at the end of the filename represents audio information I ... Not all regular expression systems are the same. ... closing parenthesis followed by a closing parenthesis ')'. ... That will work if your regular expression engines support it. ...
    (comp.sys.mac.system)
  • Suggestion for a new regular expression extension
    ... What would be really, really neat, would be a regular expression extension ... The standard parenthesis operator return the matched ... Maybe forward references would be too difficult to handle. ...
    (comp.lang.python)
  • Re: debugger exiting
    ... The debugger exits probably after a regex match fail. ... The reason is that you have a right parenthesis without a left parenthesis to the left of it. ... regular expression about where the problem was discovered. ...
    (perl.beginners)