qsl has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am having a regular expression,like
$line ="| br_summer            | schedule active   |"
I have to match the pattern "| br_summer"
.I tried like this I never got it till now.See my code
$line = "| br_summer | schedule active |"; $str="br_summer"; if($line =~ /^|\s$str /){ print"Line is $line\n"; }

Expected ouput:
Line is | br_summer            | schedule active   |
Where i am mistake monks?
Thanks,
qsl.

Replies are listed 'Best First'.
Re: Regular Expresson How to Match
by davorg (Chancellor) on Jun 27, 2006 at 15:56 UTC

    It works as expected here. I get the "Line is..." output. What are you seeing?

    Of course, it's not working for exactly the reasons that you think it is. '|' is a special character in regexes and needs to be escaped (with a backslash) if you don't want it to mean "or".

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Regular Expresson How to Match
by davido (Cardinal) on Jun 27, 2006 at 16:00 UTC

    In what way was the output different from your expected output? When I test the code, the output is exactly as you posted your expected output.

    However, just because your snippet produced the expected output in the test case you provided doesn't mean the regexp is free of bugs. The '|' character has special meaning within regular expressions. It means "alternation" or "or". Only if you preceed it with a backslash, as in '\|' will it be interpreted as a plain old character within a regular expression:

    $line =~ /^\|\s$str\s/

    ...would probably be closer to your goal.

    You'll be much more productive with regular expressions if you take an hour to read through perlretut and perlre. There's a mountain of information to learn, but those documents provide a great start.


    Dave

Re: Regular Expresson How to Match
by swampyankee (Parson) on Jun 27, 2006 at 16:06 UTC

    Let me get this straight:
    You're trying to match | br_summer and expect the output to have all the extra whitespace removed? Presuming this is a typo, not an expectation, your regex is incorrect. The pipe symbol is meaningful in a regexp,so it must be escaped, e.g.,

    if($line =~ /^\|\s$str /){ print "Line is $line (with $str)\n"; }

    Also, as a quibble, your regex won't match (even after the pipe symbol is escaped) if $str is followed by a non-blank whitespace character, e.g. a tab.

    emc

    e(π√−1) = −1
Re: Regular Expresson How to Match
by ptum (Priest) on Jun 27, 2006 at 16:02 UTC

    Although it isn't really what you are asking, have you considered using split instead of a regular expression? It looks to me as though you have a line with fields delimited by the pipe character ... often a good place to use split. You may still choose to apply a regular expression to a smaller part of the line, but you'll generally find less room for error when you have reached a more granular level.


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: Regular Expresson How to Match
by qsl (Scribe) on Jun 27, 2006 at 16:01 UTC
    Hi,
    I run your code and working now.Thanks.
A reply falls below the community's threshold of quality. You may see it by logging in.