Reputation: 37
In my equation, I'm trying to remove the first word that appears after a specific word that I have mentioned in the regex code. For example:
In the sentence:
Hello my name is Roger
I'm extracting the word "Roger" with this regex:
Regex reg3 = new Regex("is\\s\\w+");
MatchCollection match3 = reg3.Matches(result);
foreach (Match match in match3)
{
var val = match.Value;
val = val.Replace("is", "");
val = val.Trim();
}
But in a case such as this:
My answer is 3.05
and when I apply the same regex, I end up with this:
3
That is because the regex removes all characters after the "is", how do I change the regex so that it in a case such as what I mentioned above, it keeps the decimal
number as it is and prints 3.05?
Upvotes: 1
Views: 69
Reputation: 174706
It stops after printing the first number because the following .
is not a word character. To match also the literal dot, you need to put \w
and .
inside a character class.
Regex reg3 = new Regex("is\\s[.\\w]+");
OR
Regex reg3 = new Regex(@"is\s[.\w]+");
Upvotes: 2