Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: RegEx related line split

by JavaFan (Canon)
on Nov 14, 2011 at 11:41 UTC ( #937925=note: print w/replies, xml ) Need Help??


in reply to Re^2: RegEx related line split
in thread RegEx related line split

Do you understand foo|bar? Do you understand $? Do you understand (?= )? Combine all three and you get (?=$|MARK).

Replies are listed 'Best First'.
Re^4: RegEx related line split
by remiah (Hermit) on Nov 14, 2011 at 12:40 UTC
    Try to explain myself.

    foo|bar is foo or bar. if it is grouped by (foo|bar), the matched $1 will be set to "foo" or "bar".

    In this case ... it is not "non capturing grouping" (?foo|bar), because it is zero width look ahead assertion '(?='. Zero width look ahead assertion works like place holder and it does not eat up pos($expr) in matching.

    $ is the end of line... as far as I know.

    Well, it says look ahead for "end of line" or MARK and match against them as 'place holder'. I think I understand this!

    #!/usr/bin/perl use strict; use warnings; my $RefLine = "(a) This is first line(once all 4 was one line). (b) Th +is is second line; ( print "original -----\n"; print "$RefLine\n"; print "original -----\n\n"; print "\n## without 'end of line or' condtion. last line fails\n"; while( $RefLine =~ /(\([a-z]\).*?)(?=\([a-z]\))/g ){ my $p=pos $RefLine; print "$-[0], $p,matched=$&\n"; print "---\n"; } print "\n## without lookahead assertion... \n"; while( $RefLine =~ /(\([a-z]\).*?)($|\([a-z]\))/g ){ my $p=pos $RefLine; print "$-[0], $p,matched=$&\n"; print "---\n"; } print "\n## with 'end of line or' condtion and zero width place holder +\n"; while( $RefLine =~ /(\([a-z]\).*?)(?=$|\([a-z]\))/g ){ my $p=pos $RefLine; print "$-[0], $p,matched=$&\n"; print "---\n"; }

    Thank you very much JavaFan.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://937925]
help
Chatterbox?
[Eily]: and Hilarity Ensues
Eily says, while failing to write a link properly
[Your Mother]: Goodbye Fancy Feast! Hello Friskies!!!
[LanX]: CB sidebar auto-expansion problem
[Eily]: "tail: warning: following standard input indefinitely is ineffective". I can't help but feel that tail is judging me...
[Your Mother]: Someone had to say it. Better it came from tail.
[LanX]: Cat Mama!
[Your Mother]: Das right.
[Eily]: actually I forgot to provide the filename as a parameter
LanX it reeks.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2017-03-27 16:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.