Reputation: 5642
So right now I'm using this simple method to check a string for another string:
System.Text.RegularExpressions.Regex.IsMatch(toSearch, toVerifyisPresent, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
Now, its working fine for the most part. But my biggest issue is that if I try to search for something like "areyou+present", if "areyou+present" IS there, it will still come back false. I'm thinking its because of the "+" in the string.
What can I do to work around this?
Upvotes: 0
Views: 420
Reputation: 91472
Demo in IronPython:
>>> from System.Text.RegularExpressions import *
>>> Regex.IsMatch("This is a sentence containing 'areyou+present'", "areyou+present");
False
>>> Regex.IsMatch("This is a sentence containing 'areyou+present'", "areyou\\+present");
True
>>> Regex.IsMatch("This is a sentence containing 'areyou+present'", "areyou[+]present");
True
>>> Regex.IsMatch("This is a sentence containing 'areyou+present'", Regex.Escape("areyou+present"));
True
Upvotes: 1
Reputation: 2671
Based on the comment above by Oded.
toSearch.toLowerCase().Contains(toVerifyIsPresent.toLowerCase())
Converting both to lowercase will provide the same functionality as using IgnoreCase
Upvotes: 2
Reputation: 2958
In regular expressions +
matches the previous group one or more times so the regex areyou+present
matches:
areyoupresent
areyouupresent
areyouuuuuuuuuuuuuuuuuuuuuuuuuuupresent
etc...
Upvotes: 1
Reputation: 85056
You can escape special characters using a \
. But as Oded points out if you are just checking to see if a string contains something you are better off using the String.Contains
method.
Special Characters in regex:
http://www.regular-expressions.info/characters.html
String.Contains method:
http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx
Upvotes: 3