Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
The stupid question is the question not asked
 
PerlMonks  

How to use \l and \u in regex mathing patterns?

by pat_mc (Pilgrim)
on May 09, 2012 at 16:22 UTC ( #969657=perlquestion: print w/ replies, xml ) Need Help??
pat_mc has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, esteemed Monks!

I am having trouble with the upper and lower case backslash escapes in regular expressions and would very much appreciate your help with these.

So far, I thought \l matches lowercase characters, similar to [a-z] would for English text. This is obviously wrong:
$ perl -e '( $a = "hello" ) =~ s/^\l//; print $a' hello
I then thought it acts like an operator to modify substitution strings:
$ perl -e '( $a = "Hello" ) =~ s/(.+)/\l$1/; print $a' hello

This seems to work. An analogous result is obtained for \u

My question is: Can I used those escape characters in a matching pattern as well - and if so, what is their meaning in that context?

Really appreciate your wisdom.

Cheers -

Pat

Comment on How to use \l and \u in regex mathing patterns?
Select or Download Code
Re: How to use \l and \u in regex mathing patterns?
by MidLifeXis (Prior) on May 09, 2012 at 16:31 UTC

    From perlre, \l lowercases the next character, , and \L lowercases until the next \E. It does not match anything.

    --MidLifeXis

Re: How to use \l and \u in regex mathing patterns?
by AnomalousMonk (Monsignor) on May 09, 2012 at 16:36 UTC

    The  \l and  \u (and also  \L \U) operators are interpolation control operators. For upper- and lower-case regex character sets, see  [[:upper:]] and  [[:lower:]] among others, e.g., the Unicode classes. See 'Escape Sequences' section in perlre, also perlrecharclass.

Re: How to use \l and \u in regex mathing patterns?
by Anonymous Monk on May 09, 2012 at 16:41 UTC
    See perlrecharclass, perluniprops.
    $ perl -e'print "hello" =~ /\p{Lowercase}+/' 1 $ perl -e'print "HELLO" =~ /\p{Uppercase}+/' 1 $ perl -e'print "Hello" =~ /\p{Uppercase}\p{Lowercase}+/' 1
Re: How to use \l and \u in regex mathing patterns?
by brx (Pilgrim) on May 09, 2012 at 16:45 UTC

    $ perl -e '$b="H";( $a = "hello" ) =~ s/^\l$b/Y/; print $a' Yello

    see perlre: \l   lowercase next char

    So, after interpolation, "\l$b" gives "\lH" ie "h" and you obtain s/h/Y/

      What an ugly hack.

      If someone asks "How do I X with Y?", and you recognise that Y is a particularly bad way to accomplish X, then you are supposed to answer with the better way Z. If taking the question at face value, at least mention the caveats of Y.

        I don't think it was a hack, ugly or otherwise. I think it was intended, and succeeds, as an example of the actual way interpolation control operators can work in a regex, which was the context of the original – albeit misguided, since pat_mc thought of them as character classes – question. (Although I must admit that, had I given the example myself, I might have mentioned that those operators are rarely used in that particular way.)

        If someone asks ...

        Somebody already gave that answer

        ... then you are supposed to ...
        No, you are not.
Re: How to use \l and \u in regex mathing patterns?
by sundialsvc4 (Monsignor) on May 10, 2012 at 12:10 UTC

    I certainly don’t find the oblique comment, “Think vi in perldoc perlre to be particularly informative ... but then again, I don’t regularly use vi.   I therefore cordially suggest that I have never used them or paid any attention to them, and I do feel that I am none the worse for wear.   I would use character-classes and other such strategies to accomplish the intended result, and studiously avoid these.   But, that’s just me, perhaps.   I tend to find a way that works and that makes “obvious” sense to me, and that’s the end of it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-04-18 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (473 votes), past polls