Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: [OT] Thoughts on Ruby's new absent operator?

by LanX (Saint)
on Mar 24, 2017 at 16:59 UTC ( [id://1185808]=note: print w/replies, xml ) Need Help??


in reply to Re: [OT] Thoughts on Ruby's new absent operator?
in thread [OT] Thoughts on Ruby's new absent operator?

> Anyway, I get the idea that (?~abc) would be analogous to ^x, except that the former is for multi-character sequences.

Thanks, now I get the idea and I have to admit that I missed this feature in the past, though I can't recall when exactly.

But all useful use-cases which come to my mind involve strict boundaries, like parsing a grammar and explicitely excluding certain commands.

Like parsing a html but not wanting "a" and "img"-tags while allowing "anchor"

This would mean to use something like

 /<\s*\b(?~a|img)\b(.*)>/

and this should be achievable with

use strict; use warnings; undef $/; $_ = <DATA>; # slurp print "1: $1 - $2\n" while /<\s*(?!a\b|img\b)(\w+)\b(.*?)\s*>/g; print "2: $1 - $2\n" while /<\s*(?!a|img)(\w+)(.*?)\s*>/g; my %absent = ( a=>1, img =>1 ); while (/<\s*(\w+)(.*?)\s*>/g) { print "3: $1 - $2\n" unless $absent{$1}; } while (/<\s*(\w+)\b(??{ $absent{$1} ? '^' : '' })(.*?)\s*>/g) { print "4: $1 - $2\n"; } __DATA__ <a href='bla'> <img href='bla'> <table style=""> <anchor > <tr width=100>

1: table - style="" 1: anchor - 1: tr - width=100 2: table - style="" 2: tr - width=100 3: table - style="" 3: anchor - 3: tr - width=100 4: table - style="" 4: anchor - 4: tr - width=100

please note how important it is to repeat the delimiting \b in case 1, which might justify the use of (?~...) .

Though to be sure I'd need to test the Ruby implementation, and I'm not willing to install yet.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

PS: Choosing HTML as data grammar was - as always - unfortunate. You are free to propose something different.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found