Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Making a regex case insensitive

by Win (Novice)
on Mar 06, 2007 at 18:34 UTC ( #603480=note: print w/ replies, xml ) Need Help??


in reply to Re: Making a regex case insensitive
in thread Making a regex case insensitive

How do I stop the case insensitivity applying to '--' and ';'?


Update: ok I should start to try and think like a computer.


Comment on Re^2: Making a regex case insensitive
Replies are listed 'Best First'.
Re^3: Making a regex case insensitive
by johngg (Abbot) on Mar 06, 2007 at 21:17 UTC
    While the case of '--' and ';' seems to be a matter of debate and jocose comment, you might like to know that you can switch case sensitivity on and off in different parts of a regular expression. You would use constructs like (?i), (?-i), (?i:text) and (?-i:text). The first two switch case insensitivity on and off respectively. The second two just apply their effect, insensitive or sensitive respectively, to the text inside the parentheses. Here is a contrived example that uses a precompiled (qr{ ...}) regular expression that also uses extended syntax, the x, to allow comments and white space inside the expression.

    use strict; use warnings; my @strings = ( q{catFiSHcake}, q{DogFISHcAkE}, q{cATfishCake}, q{caTFISHcaKE}); my $rxMixed = qr {(?xi) # use extended syntax and # make case insensitive (?:cat|dog) # non-capture alternation # of cat or dog (?-i:FISH) # FISH, case sensitive # inside parentheses cake # cake, case insensitive # again }; foreach my $string ( @strings ) { print qq{$string: }, $string =~ $rxMixed ? qq{Match\n} : qq{No match\n}; }

    Here's the output.

    catFiSHcake: No match DogFISHcAkE: Match cATfishCake: No match caTFISHcaKE: Match

    I hope this is of interest.

    Cheers,

    JohnGG

Re^3: Making a regex case insensitive
by davorg (Chancellor) on Mar 06, 2007 at 19:43 UTC
    ok I should start to try and think like a computer

    Thinking like a programmer would be a good start.

    Update: Yes, I know it's all very tempting to downvote this. I'm sure I could have been a lot more tactful. But before you reach for the downvote button, please take a couple of minutes to review Win's posting history.

      Thinking like a programmer would be a good start.

      Fixed.

        I smell a future Turing test winner
Re^3: Making a regex case insensitive
by imp (Priest) on Mar 06, 2007 at 18:36 UTC
    I didn't know there was a lowercase '--' or ';'...
      Sure there is!

      A lowercase "--" is "__" and a lowercase ";" is ",".

      </tongue-in-cheek>


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re^3: Making a regex case insensitive
by ikegami (Pope) on Mar 06, 2007 at 18:38 UTC
    eh?? What's a lowercase and uppercase ;?
Re^3: Making a regex case insensitive
by scorpio17 (Monsignor) on Mar 06, 2007 at 18:42 UTC
    This characters will not be modified (nor will numbers, if you had any).
      Nothing is modified, not even letters. Case is preserved.
      >perl -le "'aA' =~ /(.*)/i && print $1 aA

      Only the matching is affected. /i causes lower- and upper-case letters to match lower- and upper-case versions of itself.

      Maybe you were thinking of

      >perl -le "lc('aA') =~ /(.*)/ && print $1 aa
Re^3: Making a regex case insensitive
by thezip (Vicar) on Mar 07, 2007 at 05:34 UTC

    Please Win,

    How do I stop the case insensitivity applying to '--' and ';'?

    Update: ok I should start to try and think like a computer.


    When you say things like this, it really sounds like you're just trolling...


    Where do you want *them* to go today?
Re^3: Making a regex case insensitive
by eric256 (Parson) on Mar 07, 2007 at 16:24 UTC

    Regardless of how you think you could at least test stuff.

    for (0..255) { print "$_=" . chr($_) . "\n" if chr($_) =~ /;/i };

    That should print 59=; out showing that /;/i is only matching ; and not lowercase ;, whatever that would be.

    print uc(';'), lc(';'); #outputs ;;

    FYI I like to use perl -dex (as recommended by tye I think) which gives you a nice way to run perl code and see its results.


    ___________
    Eric Hodges

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (16)
As of 2015-07-28 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls