[Chicago-talk] Regular expression question

Jonathan Rockway jon-chicagotalk at jrock.us
Sat May 2 13:04:49 PDT 2009


* On Fri, May 01 2009, Andrew Rodland wrote:
> On Friday 01 May 2009 06:08:29 pm Michael Potter wrote:
>>  Mongers,
>>
>> What is the cleanest way to combine these three regular expressions:
>>
>> $Note =~ s/^Eft /EFT /;
>> $Note =~ s/ Eft / EFT /;
>> $Note =~ s/ Eft$/ EFT/;
>
> This gives _nearly_ the same behavior and might work within your spec:
>
> s/\bEft\b/EFT/;
>
> More complicated and not tested but _should_ match your question exactly:
>
> s/(?:^|(?< ))Eft(?:$|(?= ))/EFT/;

If the \b regex doesn't work, how about s/(^| )Eft( |$)/$1EFT$2/g ?
That is usually how I represent this sort of operation.  (If you want to
keep part of the input, just capture it and put it into the output
explicitly.  [Perl 5.10 also has \k, but that won't work in this case.])

There is one difference though.  Your original regexs would change the
input:

   Eft blah blah Eft blah blah Eft

to

   EFT blah blah EFT blah blah EFT

(since each regex is allowed to match once, and each does).

Without /g, my regex would change that to:

   EFT blah blah Eft blah blah Eft

Also, can someone unban my real address from the list?  Now that a
certain Reign Of Terror has ended, I think this is just an
administrative error.

Regards,
Jonathan Rockway

--
print just => another => perl => hacker => if $,=$"


More information about the Chicago-talk mailing list