Gossamer Forum
Home : General : Perl Programming :

regex again......

Quote Reply
regex again......
I couldn't find an answer to this on any other sites so I thought I'd ask you kind folks again :)

Is there any difference between something like.....

s/Ca(n|t|p)/i;

and.....

s/Ca[ntp]/i;

Also I've seen s/something/gie; used but I've looked at loads of regex webpages but none mention "e" - what is e for?.....I know g is for global and i is non-case-sensitive.

Thanks :)

Paul
Installations:http://wiredon.net/gt/
Support: http://wiredon.net/forum/

Quote Reply
Re: regex again...... In reply to
using () would put the character into a buffer...

also.. using sed.. you'd n eed another slash... s/Ca(n|t|p)//i;

umm.. just an example of their differences..

the first one can be used to do things like this..
s/Ca(n|t|p|)/Ra\1/i;

\1 or $1.. same thing.. anyways.. it makes it Ran, Rat, Rap depending on which one it found..

s/Ca[ntp]/asdf/i;

would just always replace can, cat, cap with asdf.. no buffers..

Jerry Su
widgetz sucks
Quote Reply
Re: regex again...... In reply to
mm..

s/Ca(n|t|p)//i

is the same as

s/Ca([ntp])//i

Jerry Su
http://www.jsu07.com
Quote Reply
Re: regex again...... In reply to
From Perl5 by example http://www.codebits.com/p5be/ch10.cfm

Options for the Substitution Operator

e This option forces Perl to evaluate the replacement pattern as an expression.

g This option replaces all occurrences of the pattern in the string.

i This option ignores the case of characters in the string.

m This option treats the string as multiple lines. Perl does some optimization by
assuming that $_ contains a single line of input. If you know that it contains multiple
newline characters, use this option to turn off the optimization.

o This option compiles the pattern only once. You can achieve some small
performance gains with this option. It should be used with variable interpolation only
when the value of the variable will not change during the lifetime of the program.

s This option treats the string as a single line.

x This option lets you use extended regular expressions. Basically, this means that
Perl ignores whitespace that is not escaped with a backslash or within a character
class. I highly recommend this option so you can use spaces to make your regular
expressions more readable.

The /e option changes the interpretation of the pattern delimiters. If used, variable interpolation is active even if single quotes are used. In addition, if back quotes are used as delimiters, the replacement pattern is executed as a DOS or UNIX command. The output of the command is then used as the replacement text.

Bob
http://totallyfreeads.com

Quote Reply
Re: regex again...... In reply to
Thanks Jerry and lanerj

Paul
Installations:http://wiredon.net/gt/
Support: http://wiredon.net/forum/