Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Regex Tool

by Yunus (Novice)
on Jun 30, 2006 at 02:09 UTC ( #558495=note: print w/ replies, xml ) Need Help??


in reply to Re: Regex Tool
in thread Regex Tool

Select the anchor options, will automatically put '^' or '$' or '^$'. you're allowed to put only once per regex string (that's the regex rule, isn't it?).

But there's a few bugs i.e. if you create sub regex, you can create something absurd like this ' /^item(^duhduh$) foo$/'. i'm still looking into it. sorry for that. (i hold this quote for a while...)


Comment on Re^2: Regex Tool
Re^3: Regex Tool
by Dietz (Curate) on Jun 30, 2006 at 06:53 UTC

    Just for your interest:

    Multiple ^ or $ anchors would be legal.
    The /m modifier in combination with the /s modifier would match the intended behaviour.
    /m: match over multiple lines
    /s: . matches everything (even newlines)

    Note that you would have to insert code to match any newline either by . or by \s. See example below.

    my $text = <<"END"; a sample line item duhduh foo END $text =~ /^item.*(^duhduh$)\s*foo$/sm; print "\$1: '$1'\n"; # prints "$1: 'duhduh'"

      Just a point here - something I finally got only after a number of rereadings of the section in J. Friedl's excellent Mastering Regular Expressions (O'Reilly) the /m and /s are a little more complicated.
      /m: better remembered as 'multi' mode - affects 'multiple' (2) meta chars, the anchors (^ and $)
      /s: 'single' mode - affects one meta char, the dot '.'

      /s changes the dot's normal definition - match any char except a new line (\n). In single mode the dot can match \n too which allows regex phrases like:
      .*

      to match across the end of line. That all it does and so its why the "item.*" in your example matches the end of line.
      /m changes the ^ and $ anchors from absolute beginning and end of string to match beginning and end of a line, as marked by new line chars. Which is useful w/ the '/g' option, for example:

      local $/ = undef; my $whole_file = <>; # slurp while ( $whole_file =~ /^(.*)$/mg ) { # process line by line print "Got: $1\n"; }
      Not a useful snippet but ... two notes - the '\n' on the print stmt and notice the diff if you put an 's' in the match options

      a

Re^3: Regex Tool
by eric256 (Parson) on Jun 30, 2006 at 14:49 UTC

    That doesn't explain why its adding \< and \> in my regex.


    ___________
    Eric Hodges
      Uncheck the 'Begin of word' 'End of word' (last column). This is the option for word boundry. While the string boundry /line boundry set using the anchor option..

        Okay I might not have been clear. Why, in with any option checked, is it adding \<. Is that a regex command that I just don't know? The only time i've seen \< in a reged it meant a literal < not something special.


        ___________
        Eric Hodges

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://558495]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-12-18 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls