[Melbourne-pm] case insensitive REs

Jacinta Richardson jarich at perltraining.com.au
Tue May 6 21:16:09 PDT 2008


Tim Connors wrote:
> G'day.
> 
> I want the user to be able to supply a -i flag to my program to make 
> global case insensitive searching.
> 
> Except that when I actually go to perform the RE operation in perl, it 
> only takes /i as a modifier.  I can't simply say, where $case contains 
> either "i" or "":
> 
>   while (/($re)/g$case) {
>      ...
>   }

You can put modifiers inside regular expressions too.

	while(m/(?i:($re)/g) {
		...
	}

Ever wondered why non-capturing braces look so ugly?  (?: ...  ) now you know!

	Here's a test:

	my $foo = "ABC";
	my $bar = "abc";

	foreach ($foo, $bar) {
	        print "matched $_\n" if m/(?i:A)/;
	}

	matched ABC
	matched abc

All the best,

	J


-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |


More information about the Melbourne-pm mailing list