Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Regex: Char-class equivalent for [^]

by LanX (Canon)
on Jan 14, 2013 at 13:49 UTC ( #1013215=perlquestion: print w/ replies, xml ) Need Help??
LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I read once that JS-regexes follow the standards of Perl4.

Now I was quite surprised to learn that JS doesn't have a /s single-line modifier to let '.' also match on linebreaks like \n.

As a compensation JS offers to use [^] to match everything (the negation of nothing is everything).

>>> "a <\n \n> z".match('<[^]*>') ["<\n \n>"]

I like the concept and was wondering if there is any equivalent in Perl ...

Here [^] is a syntax error because at this position Perl magically expects ']' to be part of the negated char-class and still expects another closing ']'.

The closest that I was able to find was (?:.|\n)

DB<118> "a <\n \n> z" =~ /<(?:.|\n)*>/ ; $ & => "<\n \n>"

Any other suggestions?

Cheers Rolf

Update

Another option is to use single-line locally: (see perlrecharclass)

DB<146> "a <\n \n> z" =~ /<(?s:.)*>/;$ & => "<\n \n>"

not shorter but cleaner!

Comment on Regex: Char-class equivalent for [^]
Select or Download Code
Re: Regex: Char-class equivalent for [^]
by BrowserUk (Pope) on Jan 14, 2013 at 14:01 UTC

    How about one of these?

    "the quick brown fox\njumps over the lazy dog" =~ m[([\W\w]+)] and pri +nt $1;; the quick brown fox jumps over the lazy dog "the quick brown fox\njumps over the lazy dog" =~ m[([\S\s]+)] and pri +nt $1;; the quick brown fox jumps over the lazy dog "the quick brown fox\njumps over the lazy dog" =~ m[([\D\d]+)] and pri +nt $1;; the quick brown fox jumps over the lazy dog

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hmm, bundling complementary char-classes is a clever idea!

      thx! :)

      Cheers Rolf

      UPDATE: and works equally in both languages!

Re: Regex: Char-class equivalent for [^]
by Anonymous Monk on Jan 14, 2013 at 14:01 UTC
    So a character class that matches everything? Did you check perlrecharclass

    \p{All} and not all is \P{All}

    \p{All} in javascript is  [\u0000-\u10FFFF] and not all [^\u0000-\u10FFFF]

    in perl  [\x{U+0000}-\x{U+10FFFF}]  [^\x{U+0000}-\x{U+10FFFF}]

      > Did you check perlrecharclass

      Yes i did. Did you?

      p{All} isn't mentioned there

      Anyway googling led me to perluniprops, a perldoc not available for 5.10.

      Cheers Rolf

        :) well, perlrecharclass links perluniprops

        aslo, shhould have bee  [\N{U+0000}-\N{U+10FFFF}]  

        and for javascript \u only takes 16 bits, whoops :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013215]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-09-17 23:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (100 votes), past polls