in reply to Regular Expression - pattern matching

kulls,

You've provided some sample data - good!
You've provided the code that you've written so far - excellent!
But - have actually told us what the code is supposed to do?
Some pattern matching obviously, but what are the rules?

Once again, you're asking people to second-guess what it is that you are trying to do. If you expect sensible and helpful answers to your questions, you need to be more explicit.

For example, you might have said: "If the string starts with two word characters, I want to extract the first 13 characters, otherwise I want to extract the first 27 characters"

(That obviously isn't what you are trying to do with your code, but hopefully you get the idea)

Cheers,
Darren :)

  • Comment on Re: Regular Expression - pattern matching

Replies are listed 'Best First'.
Re^2: Regular Expression - pattern matching
by kulls (Hermit) on Feb 22, 2006 at 09:22 UTC
    Hi Darren,

    I need to get the preferred patterns from the  @str array.As i don't have a any general/common rule to due to different patterns,i guess the following assumption can yield the good results.


    My assumptions are,

    #. check the number of characters before the first "_" (underscore).
    #. If it's return only two characters, then get the string untill it reach the second "_"(underscore) from the string else if more than 2 characters then it must be a targetted pattern.
    #. I guess i can't able to use the "\w" (word) pattern, because it will take the whole string as a word.

    Thank you in advance.
    -kulls
      #. check the number of characters before the first "_" (underscore). #. If it's return only two characters, then get the string untill it reach the second "_"(underscore)
      Okay, well because you are looking for anything that is not an underscore, then a negated character class is probably the way to go. Something like this:
      if (/^([^_]{2}_[^_]+)_/) { print $1; }
      else if more than 2 characters then it must be a targetted pattern
      Sorry, but I don't get that. What is a "targetted pattern"?

      I guess i can't able to use the "\w" (word) pattern, because it will take the whole string as a word.
      Yes and no. It's okay, because you are limiting it to only the first two characters with {2}. But it's probably not okay because \w will also match an underscore. So \w{2} would match something like "_a", which I'm pretty sure you don't want.

      Cheers,
      Darren :)

        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