Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

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


in reply to Re^2: Regular Expression: search two times for the same number of any signs (updated)
in thread Regular Expression: search two times for the same number of any signs

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.

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

    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found