Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Making a regex case insensitive

by ikegami (Pope)
on Mar 06, 2007 at 18:18 UTC ( #603476=note: print w/ replies, xml ) Need Help??


in reply to Making a regex case insensitive

There's a modifier for regexp that makes it case-insensitive.

By the way, if you're trying to prevent the users from making changes to the database, why don't you revoke the relevant rights from your database user?


Comment on Re: Making a regex case insensitive
Re^2: Making a regex case insensitive
by Win (Novice) on Mar 06, 2007 at 18:26 UTC
    Because copies of the database exist on multiple machines and I believe that the system is best protected on two different levels. I wish to port the system to different applications as well. I can't believe that I forgot the /i thing. I really must go back to those Perl books I have at home and read them again.
      Instead of checking for bad tokens you should just use bound parameters whenever possible, and DBI's quote method when it isn't possible. You'll save yourself a lot of pain that way.

      Perfect paranoia is perfect awareness when it comes to preventing SQL injection attacks. Make sure you are binding or quoting everything that will touch the database. It's a semi common mistake to include $ENV{HTTP_REFERER} or $ENV{HTTP_USER_AGENT} in the sql unquoted.

        Are you suggesting that I should check each entry with a tight regex?

        It's a semi common mistake to include $ENV{HTTP_REFERER} or $ENV{HTTP_USER_AGENT} in the sql unquoted.

        I havenít a clue what you mean by that.
      I really must go back to those Perl books I have at home and read them again.
      I'll say.
Re^2: Making a regex case insensitive
by Win (Novice) on Mar 06, 2007 at 18:34 UTC
    How do I stop the case insensitivity applying to '--' and ';'?


    Update: ok I should start to try and think like a computer.
      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$..$/
      eh?? What's a lowercase and uppercase ;?
      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
      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.

      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

      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?

      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://603476]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-07-28 21:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls