Bihui Jin
Bihui Jin

Reputation: 13

How to extract a whole word from a sentence by a specific fragment in C#?

How can I obtain a whole word within a string-type sentence? \

For instance, if the given string was:

The app has been updated to 88.0.1234.141 which contains a number of fixes and improvements.

And I want to get the word 88.0.1234.141 by a specific fragment ".0."
How can I grab every word that contains ".0."? Is there a fast way to do it maybe like Regex instead of using nested loops?

Upvotes: 0

Views: 193

Answers (1)

Fruchtzwerg
Fruchtzwerg

Reputation: 11399

You can use a regular expression to solve this. Therefore you can use a simple approach: Take each word containing .0. inside. Each word should be surrounded by a space. The expression which results could look like this:

Visualized regex

Here is an implemented sample of the expression:

string input = "The app has been updated to 88.0.1234.141 which contains a number of fixes and improvements.";
MatchCollection matches = Regex.Matches(input, @"[^-\s]*\.0\.[^-\s]*");
foreach (Match match in matches)
{
    Console.WriteLine(match.Value);
}

The output will be

88.0.1234.141

Upvotes: 1

Related Questions