Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

help needed to solve in regex

by jesuashok (Curate)
on Jan 10, 2007 at 04:55 UTC ( #593825=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: help needed to solve in regex
by ikegami (Pope) on Jan 10, 2007 at 05:05 UTC

    You're using a string where a regular expression is expected.

    my $find_re = qr/1\.2\.3/i; ... grep /$find_re/, ... ...

    or

    my $find_str = '1.2.3'; my $find_re = quotemeta($find_str); ... grep /$find_re/i, ... ...

    or

    my $find_str = '1.2.3'; ... grep /\Q$find_str\E/i, ... ...

      You forgot the surround the pattern with \b markers.

      /\b\Q$find\E\b/

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        oh, I didn't notice that matched the third line. Unforunately, I don't agree with your solution. That is to say I don't think we can provide a solution with the information he provided us. For example, all of the following match his criteria, yet all are different:
        • /\b\Q$find\E\b/
        • /(?<!\S)\Q$find\E/(?!\S)
        • /\Q$find\E/(?!\.)
        • /\Q$find\E$/
        • /\Q$find\E$/m
        • /\Q$find\E\z/
        • ...

        The op should have a look at perlre. (Or should I say "another look" since he should already have looked there when he started having problems with his regexp...)

Re: help needed to solve in regex
by chromatic (Archbishop) on Jan 10, 2007 at 06:30 UTC

    Why grep with a one-element list rather than a standard pattern match?

Re: help needed to solve in regex
by Praveen (Friar) on Jan 10, 2007 at 05:40 UTC
    Hi
    Try this
    if ( grep ( /\Q$find\E$/i , $input_line ) ) {
Re: help needed to solve in regex
by wazoox (Prior) on Jan 10, 2007 at 12:03 UTC

    The problem is that "." matches any character in regexes, so

    /1.2.3/
    Actually means match "1" followed by any character then "2" then any character then "3" . What others suggested is to escape the dots so that they only match dots, using the different ways available (because of course TIMTOWTDI).

    By the way if you need to try out your regexes in an easy and graphical way, I advise you to try the wonderful script regexperiment from paul Gaborit, I use it a lot and it saved me much time and effort.

Re: help needed to solve in regex
by murugu (Curate) on Jan 10, 2007 at 11:30 UTC

    Hi jesuashok,

    You can use quotemeta and \b

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://593825]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2021-12-08 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (35 votes). Check out past polls.

    Notices?