2

I typically have a string that might look like the following and I would like to extract the contents and parse it into an array by splitting the values.

COUNT(123,453,123)

For validation reasons it might so happen that a comma is left on the end of contents like so.

COUNT(123,453,123,)

My code so far is as follows.

Regex.Match(testString, @"(?<=\().+?(?=\))").ToString().Split(',').Select(int.Parse).ToList();

It works fine for the first case but will throw an exception on the latter case.

Exception

input string was not in a correct format

How can I make the regex ignore a comma if there is no additional number after it?

1 Answer 1

4
(?<=\().+?(?=,?\))

             ^^

Just add that to lookahead.See demo.

https://regex101.com/r/rO0yD8/14

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.