NuB: RegExp case-independent match

D

David Alm

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.
 
R

Robert Klemme

Jeffrey said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top