13

I am trying to create a match query which selects text from a string between two words. I can't seem to figure out how to make the search case insensitive. For example consider the text:

contents = "cat
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec blandit feugiat mi, eu lacinia quam tincidunt eu. Donec eleifend adipiscing neque, in porta dolor vestibulum at. Curabitur id elit vitae nunc feugiat varius. Maecenas euismod euismod mi, eu blandit lectus.
dog"

the ruby code to return me the latin code between the words cat and dog is

contents.match(/cat(.*)dog/m)[1].strip

At the moment my "match" query only works if cat and dog are lowercase but I need to cater for if cat and dog are uppercase or the first letter is upper case.

Not quite sure where to stick the /i operator.

1 Answer 1

25

Next to the "m":

contents.match(/cat(.*)dog/im)[1].strip
Sign up to request clarification or add additional context in comments.

3 Comments

I don't even think the "m" is doing anything in this expression. "m" is used to make ^ and $ match the start/end of a line, rather than the start/end of input.
@d11wtq - I just tried it in irb and it seems to make a difference. I believe it allows the . character to match newlines.
@coder_tim, ah, ok, I always use the s modifier for that ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.