Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Regular Expression: search two times for the same number of any signs (updated)

by haukex (Archbishop)
on Nov 29, 2016 at 11:02 UTC ( [id://1176791]=note: print w/replies, xml ) Need Help??


in reply to Regular Expression: search two times for the same number of any signs

Hi,

Disclaimer: I am not a regex wizzard, so I'm not sure if the following has any pitfalls, but it does appear to be possible with a single regex:

print $_, /(x(.*)x(??{ '.{'.length($2).'}' })x)/ ? " matches, \$1 = $1\n" : " doesn't match\n" for qw/ xxx x.x.x x12x..x x123x...x x1x2x...x x123x.x.x x12x1x ax1x2xbx34x56xc /; __END__ xxx matches, $1 = xxx x.x.x matches, $1 = x.x.x x12x..x matches, $1 = x12x..x x123x...x matches, $1 = x123x...x x1x2x...x matches, $1 = x1x2x...x x123x.x.x matches, $1 = x123x.x.x x12x1x doesn't match ax1x2xbx34x56xc matches, $1 = x1x2xbx34x56x

Update: Changing the first part of the regex to x(.*?)x (non-greedy) will allow you to match all the substrings in that last example above (and the rest of the examples above will continue to work the same):

my $re = qr/(x(.*?)x(??{ '.{'.length($2).'}' })x)/; my $str = "ax1x2xbx34x56xc"; while ($str=~/$re/g) { print "found \"$1\"\n"; } __END__ found "x1x2x" found "x34x56x"

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Regular Expression: search two times for the same number of any signs (updated)
by Anonymous Monk on Nov 29, 2016 at 12:41 UTC

    Hi Hauke,

    I am trying to fully understand all the new things. One question to your construct "for"

    print "$_ =>\n" for qw/ 1 12 123/;

    is working fine. I like this style. But I can not combine it with an if or multiple lines.

    {print "$_ =>\n" if $_=/1/ } for qw/ 1 12 123/;

    gives me floowing error message: "Missing $ on loop variable at ./test3.pl line 2." and I can not understand, what is mean by this error meassage.

      Hi Anonymous,

      You can't use more than one statement modifier like for or if at a time (and I think that if you were able to, it would lead to more hard-to-understand code). If your code gets more complex you should instead use a normal for loop:

      for my $n (qw/1 12 123 234/) { print "$n =>\n" if $n=~/1/; }

      (Ok, there is a way to do what you want, but legibility begins to suffer if it gets longer: /1/ and print "$_ =>\n" for qw/1 12 123 234/;)

      Update: I should add that I was golfing a little bit in my example code, and that compressed style is not necessarily something one should strive to use in production code ;-)

      Regarding the other question about (??{ }), that's documented along with (?{ }) in perlre. The oversimplified explanation is that the code inside (??{...}) is evaluated and its return value embedded as part of the regular expression (but make sure to read the docs). So in my regex, the code '.{'.length($2).'}' takes the length of the string matched in between the first set of x's (x(.*)x), and then generates an expression like .{N} (where N is the length), so if the input were x12345x67890x, the regular expression it is matched against is x.*x.{5}x.

      Hope this helps,
      -- Hauke D

      Updated wordings a little bit.

        Hi Hauke,

        Many many thanks for your explainations and tips !!!!!

        Took a while to understand, read and try out ... I really learned thinks , that are a complet new level of perl for me !

        Thanks

Re^2: Regular Expression: search two times for the same number of any signs (updated)
by Anonymous Monk on Nov 29, 2016 at 12:26 UTC

    Hi Hauke,

    Perfect solution. Exact what I wanted to have.

    But I do not understand the ?? { } part.

    Can you explain a little bit or give me a link where I can read more.

    many thanks !!!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-20 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found