Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: \1, \2, \3, ... inside of a character class

by gmpassos (Priest)
on Aug 12, 2004 at 20:49 UTC ( [id://382461]=note: print w/replies, xml ) Need Help??


in reply to \1, \2, \3, ... inside of a character class

As you said, you want \1 working as the first captured occurrence in a character class ([...]). But \1 can capture much more than a single character, so, it can't be used inside a character class because it doesn't represent a character, but a string. To match multiple strings just use (?:...|\1|...).

Graciliano M. P.
"Creativity is the expression of the liberty".

Replies are listed 'Best First'.
Re^2: \1, \2, \3, ... inside of a character class
by ccn (Vicar) on Aug 12, 2004 at 21:26 UTC
    \1 can capture much more than a single character, so, it can't be used inside a character class because it doesn't represent a character, but a string

    It's not a problem, see perlretut

    /[\]c]def/; # matches ']def' or 'cdef' $x = 'bcr'; /[$x]at/; # matches 'bat', 'cat', or 'rat' /[\$x]at/; # matches '$at' or 'xat' /[\\$x]at/; # matches '\at', 'bat, 'cat', or 'rat'
      Ok, what you want is \1 working as a group of chars. Well, we can't, but you can use some complexer code:
      $var = "abXaaabbbc" ; $var =~ s/(..)X(??{"[$1]+"})/${1}#/ ; print "$var\n" ;
      Output:
      ab#c

      Graciliano M. P.
      "Creativity is the expression of the liberty".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2024-04-19 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found