Need to get three strings from the below mentioned string, need the possible solution in C# and ASP.NET:
"componentStatusId==2|3,screeningOwnerId>0"
I need to get ‘2’,’3′ and ‘0’ using a regular expression in C#
Need to get three strings from the below mentioned string, need the possible solution in C# and ASP.NET:
"componentStatusId==2|3,screeningOwnerId>0"
I need to get ‘2’,’3′ and ‘0’ using a regular expression in C#
2
Answers
Use following regex and capture your values from group 1, 2 and 3.
Demo
For generalizing
componentStatusId
andscreeningOwnerId
with any string, you can usew+
in the regex and make it more general.Updated Demo
If all you want is the numbers from a string then you could use the regex in this code:
Function
DisplayMatchResults
is taken from this Stack Overflow answer.The Console output from the above is:
Hence the numbers can be seen in
match.Groups[1].Captures[...]
.Another possibility is to use
Regex.Split
where the pattern is "non digits". The results from the code below will need post processing to remove empty strings. Note thatRegex.Split
does not have theStringSplitOptions.RemoveEmptyEntries
of the stringSplit
method.The output from this is: