user1884032
user1884032

Reputation: 347

get values from string when characters are involved

I have a string in which I want to get the both the values "12345" and "123.5" from and put it into a collection so I can loop through and do something with it. Can someone help with this?

string test = "Hello World [12345] - [123.5]"

string anothertest = "Hello World [A12345 (05,00,45)] [518.6Z] [51.5]"

I would like "A12345" "518.6Z" "51.5"

Upvotes: 0

Views: 62

Answers (2)

frickskit
frickskit

Reputation: 624

For the second part you edited in:

\[(\d*\w*\.?\d*\w*)(?!\()

The "?!" is a negative lookahead assertion which in this case means "match the things before that are not followed by a ("

Upvotes: 0

frickskit
frickskit

Reputation: 624

Use the following regex:

\[(\d+\.?\d+)\]

You'll want group(1).

Maybe this semi-pseudo will help...

Regex expression = new Regex(@"\[(\d+\.?\d+)\]");
var results = expression.Matches(test);
foreach (Match match in results)
{
    //do whatever you want.
}

Upvotes: 2

Related Questions