http://www.perlmonks.org?node_id=1013215

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!

Replies are listed 'Best First'.
Re: Regex: Char-class equivalent for [^]
by BrowserUk (Patriarch) 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 :)