user2617874
user2617874

Reputation: 101

regular expression match case sensitive off

I have a regular expression that finds two words in a line.

The problem is that it is case sensitive. I need to edit it so that it matches both the case.

reqular expression

^(.*?(\bPain\b).*?(\bfever\b)[^$]*)$

Upvotes: 1

Views: 1465

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626690

You can use RegexOptions.Ignorecase to set case insensitive matching mode. This way you make the entire pattern case insensitive. The same effect can be achieved with (?i) inline option at the beginning of the pattern:

(?i)^(.*?(\bPain\b).*?(\bfever\b)[^$]*)$

You can use the inline flag to only set case insensitive mode to part of a pattern:

^(.*?(\b(?i:Pain)\b).*?(\b(?i:fever)\b)[^$]*)$

Or you can just match "pain" or "Pain" with

^(.*?(\b(?i:P)ain\b).*?(\bfever\b)[^$]*)$

Another alternative is using character classes [Pp], etc.

Note that you do not have to set a capturing group round the whole pattern, you will have access to it via rx.Match(str).Groups(0).Value.

^.*?(\b[pP]ain\b).*?(\b[Ff]ever\b)[^$]*$

Upvotes: 3

Amen Jlili
Amen Jlili

Reputation: 1934

Well, if you're using VB.net, you can tell the regex object to ignore the case sensitivity when you create it

     'Defines the pattern
    Dim MyPattern As String = "BlaBla"
     'Create a new instance of the regex class with the above pattern 
     'and the option to ignore the casing 
    Dim Regex As New Regex(MyPattern, RegexOptions.IgnoreCase)

Upvotes: 0

Andris Leduskrasts
Andris Leduskrasts

Reputation: 1230

You can usually set a flag for that, depending on your language, or you can mess up your regex into a more ugly looking one using multiple character classes. [pP][aA][iI][nN] is essentially the word "pain" without it being case sensitive at all.

Upvotes: 0

Related Questions