Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

regex square brackets exceptions

by raybies (Chaplain)
on Jun 21, 2011 at 13:15 UTC ( [id://910732]=perlquestion: print w/replies, xml ) Need Help??

raybies has asked for the wisdom of the Perl Monks concerning the following question:

Twas reading about java regex and noticed they had a section in the article about Character Classes using [] characters...

  • [a-z-[bc]] = a through z, except for b and c: [ad-z] (subtraction)
  • [a-z-[m-p]] = a through z, except for m through p: [a-lq-z]
  • [a-z-[^def]] = d, e, or f

I'd never seen square brackets used with such... abandon... and wondered if Perl had an equivalent way of doing such exceptions...

I tried the equivalent in Perl, but it didn't appear to match as they indicate from the Java world.

Replies are listed 'Best First'.
Re: regex square brackets exceptions
by moritz (Cardinal) on Jun 21, 2011 at 13:59 UTC
    There's an easy way to check: try it out. There's another one that I like:
    $ perl -Mre=debug -ce '/[a-z-[bc]]/' Compiling REx "[a-z-[bc]]" Final program: 1: ANYOF[\-[a-z][] (12) 12: EXACT <]> (14) 14: END (0) anchored "]" at 1 (checking anchored) stclass ANYOF[\-[a-z][] minlen 2 -e syntax OK Freeing REx: "[a-z-[bc]]"

    It tells you that the final closing square bracket is interpreted as a literal (EXACT) match, so it surely didn't parse the regex class the same way as Java does.

Re: regex square brackets exceptions
by ww (Archbishop) on Jun 22, 2011 at 00:40 UTC
    Are these acceptably comparable?

    They don't use a single bit of syntax, of course, but it's not clear to me from your question if you're seeking syntax or simply an method to exclude characters.

    perl -e "use 5.012; my $foo='now is the time for'; if ( $foo =~ /[meit +]+/ && $foo =~ /[^a]{1,20}/ ) { say 'true';} else {say 'nope';}" true

    Similarly, not a single phrase, but...

    perl -e "use 5.012; my $bar = 'abcdefg'; if ($bar =~ /[a-d]/ && $ bar +!~ /[ef]/ ){ say 'true';} else {say 'false';}" false
Re: regex square brackets exceptions
by Nikhil Jain (Monk) on Jun 21, 2011 at 13:22 UTC

    Have a look at perlrequick:character classes , you will come to know to how to write character class in Perl.

      Actually I was already familiar with character classes as per that documentation... I'm teaching regex, and am interested in the differences between Perl and Java because I've got java programmers in the audience.

      Perl being what it is... one never knows if there isn't some little bit of syntactic sugar hidden away somewhere...

      But the short answer is, "No."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found