CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
regular expressions dummy

 



parham_m_s
journeyman

Jan 6, 2001, 7:44 PM

Post #1 of 3 (1246 views)
regular expressions dummy Can't Post

i'm horrible at regular expressions, and i have a question to ask...

Code
s/a//g; 
s/b//g
s/c//g;

is the same as

Code
s/[abc]//g; and s/[abc]+//;

is that wrong? is there a better way of doing it?



rGeoffrey
User

Jan 7, 2001, 9:51 PM

Post #2 of 3 (1243 views)
Re: regular expressions dummy [In reply to] Can't Post

To globally remove all 'a' from the string...
s/a//g;
To globally remove all 'b' from the string...
s/b//g
To globally remove all 'c' from the string...
s/c//g;

To globally remove all 'a' or 'b' or 'c' from the string...
s/[abc]//g;

That line should get the same results as doing the other three lines.

But this last line will produce something different...
s/[abc]+//;

It will only make one substitution. The first time it finds some combination of consectutive 'a' or 'b' or 'c' it will remove them. But it will then stop looking.

So these strings will get the same results at the earlier versions...
eeeeeeaaabbcbcceeeeee
eeeeeebeeeeeeee
eeaeeee
acbcbcabcabcbababccee

But these will only catch the first group...
eeabceeeeeabcabcabc
eeaaaaeeeebbbbbcbcbcbcbc

So s/[abc]//g; is probably the best choice here There can be cases where you will get better performance using the three separate substitutions, but usually you won't notice the improvement.



japhy
Enthusiast / Moderator

Jan 8, 2001, 12:18 PM

Post #3 of 3 (1240 views)
Re: regular expressions dummy [In reply to] Can't Post

Ah, my favorite question. :)

Using s/[abc]//g is slower than using s/[abc]+//g. But the fastest way of all, on average, is tr/abc//d.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives