[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

NuB: RegExp case-independent match

David Alm

4/28/2006 5:41:00 PM

I'm new to both Ruby and RegExp.

I'm wanting to do a string sub in lines of a file but have the match
criteria for gsub be case-independent. The only think I could think of was
to upcase the whole thing:

content = File.readlines( x ).collect! { |line|
if ( line =~ /someLineCriteria.*$/ )
line.upcase!.gsub(/SOME_GSUB_CRITERIA/, $string_to_sub )
else
line
end
}

But I would prefer not to have to upcase it all, and have the RE match
"SomE_Gsub_CRITeria" or any similar combination of upper/lower case.

Thanks in advance.

-----
Dave
Herd Master
Round Cow Systems
www.roundcow.com



2 Answers

Jeff Schwab

4/28/2006 5:47:00 PM

0

David Alm wrote:
> I'm new to both Ruby and RegExp.
>
> I'm wanting to do a string sub in lines of a file but have the match
> criteria for gsub be case-independent. The only think I could think of was
> to upcase the whole thing:
>
> content = File.readlines( x ).collect! { |line|
> if ( line =~ /someLineCriteria.*$/ )
> line.upcase!.gsub(/SOME_GSUB_CRITERIA/, $string_to_sub )
> else
> line
> end
> }
>
> But I would prefer not to have to upcase it all, and have the RE match
> "SomE_Gsub_CRITeria" or any similar combination of upper/lower case.
>
> Thanks in advance.

After the last slash of the regex literal, add the modifier "i":

/someLineCriteria.*$/i

Robert Klemme

4/28/2006 6:51:00 PM

0

Jeffrey Schwab wrote:
> David Alm wrote:
>> I'm new to both Ruby and RegExp.
>>
>> I'm wanting to do a string sub in lines of a file but have the match
>> criteria for gsub be case-independent. The only think I could think of
>> was to upcase the whole thing:
>>
>> content = File.readlines( x ).collect! { |line|
>> if ( line =~ /someLineCriteria.*$/ )
>> line.upcase!.gsub(/SOME_GSUB_CRITERIA/, $string_to_sub )
>> else
>> line
>> end
>> }
>>
>> But I would prefer not to have to upcase it all, and have the RE match
>> "SomE_Gsub_CRITeria" or any similar combination of upper/lower case.
>>
>> Thanks in advance.
>
> After the last slash of the regex literal, add the modifier "i":
>
> /someLineCriteria.*$/i

Also it's completely useless to use ".*$" here - it won't make a
difference with regard to matches and only waste CPU cycles.

Regards

robert