in reply to Re^3: Regular Expression - pattern matching
in thread Regular Expression - pattern matching

Hi Darren,
I'm looking for the pattern like "xxxxx" if it's "xxxx_sssss" and also if it's "xx_ssss" then i need "xx_ssss" .
I have reuse your code
if (/^([^_]{2}_[^_]+)_/) { print $1; }
and it looks like
for(@str) { my $temp; if (/^([^_]{2}_[^_]+)_/) { $temp=$1; print $temp."\n"; } else { ($temp)=$_=~/^([a-z0-9]+)\_/xi; print $temp."\n"; } }

Unfortunately i didn't get the exact pattern which i showed in my output earlier.
Can you please correct me if i'm wrong ??
Thank you in advance.
-kulls

Replies are listed 'Best First'.
Re^5: Regular Expression - pattern matching
by McDarren (Abbot) on Feb 22, 2006 at 17:55 UTC
    Okay, after scratching my head for about 20 minutes, I think I've worked out what you want.

    • If the first two characters are not underscores, but the third character is - then keep everything up to (but not including) the second underscore. Otherwise...
    • Keep everything up to (but not including) the first underscore.
    Is that correct?
    If yes, then the following should do it:
    for (@strings) { my $temp = ""; if (($temp) = $_ =~ m/^([^_]{2}_[^_]+)_?/) { print $temp; } elsif (($temp) = $_ =~ m/^([^_]+)/) { print $temp; } }
    Update: Oh, one thing I forgot to point out, was that there was an error in the code in my earlier reply. I hadn't accounted for the fact that there may not be any underscores at all. That's been fixed in the above snippet.