Reputation: 2201
I want a Regular Expression for a word.otherword form. I tried \b[a-z]\.[a-z]\b
, but it gives me an error at the \.
part, saying Unrecognized escape sequence
. Any idea what's wrong? I'm working under .NET C#. Thanks!
LE:
john.Smith
or JoHn.SmItH
or JOHN.SMITH
should work.
John Smith
or john!Smith
or john.Smith.Smith
shouldn't work.
Upvotes: 1
Views: 96
Reputation: 26940
Try this :
foundMatch = Regex.IsMatch(SubjectString, @"\b[a-z]\.[a-z]\b");
Probably you were not using @?
Your regex tries to match a.a this means a single character. But since you want it to match complete words you need a quantifier e.g.
\b[a-z]+\.[a-z]+\b
Finally you may want to use the case insensitive match to allow for words with capital letters to be matched too :
foundMatch = Regex.IsMatch(SubjectString, @"\b[a-z]+\.[a-z]+\b", RegexOptions.IgnoreCase);
This will match all words.words with at least one character for each word regardless of capitalization.
This will match all word.otherword only if there is a space behind the first word or it is the start of the string and only if there is a space after the second word or it is the end of the string.
foundMatch = Regex.IsMatch(SubjectString, @"(?<=\s|^)\b[a-z]+\.[a-z]+\b(?=\s|$)", RegexOptions.IgnoreCase);
Upvotes: 6
Reputation: 16544
Try this regex for word.word
format:
@"\b([a-z]+)\.\1"
For word.otherword
use this:
@"\b[a-z]+\.[a-z]+\b"
Upvotes: 1