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. | [reply] |
|
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
|
|
|
| [reply] |
|
I really must go back to those Perl books I have at home and read them again.
I'll say.
| [reply] |
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. | [reply] |
|
I didn't know there was a lowercase '--' or ';'...
| [reply] |
|
| [reply] [d/l] [select] |
|
eh?? What's a lowercase and uppercase ;?
| [reply] [d/l] |
|
This characters will not be modified (nor will numbers, if you had any).
| [reply] |
|
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
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
Thinking like a programmer would be a good start.
Fixed.
| [reply] |
|
|
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 | [reply] [d/l] [select] |
|
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?
| [reply] |
|
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.
| [reply] [d/l] [select] |