Eni Eni -4 years ago 100
C# Question

Getting string between characters in Regex

I have a list of strings with the output below

stop = F6, quantity ( 1 ) // stop 0
stop = F8, quantity ( 1 ) // stop 1
stop = BN, quantity ( 1 ) // stop 2
stop = F6, quantity ( 1 ) // stop 3
stop = F8, quantity ( 1 ) // stop 4
stop = BN, quantity ( 1 ) // stop 5
stop = F6, quantity ( 1 ) // stop 6
stop = F8, quantity ( 1 ) // stop 7
stop = SC, quantity ( 1 ) // stop 8
etc


using a foreach loop i'm retrieving each line in the list ie

`stop = F6, quantity ( 1 ) // stop 0`


However I only need the character F6.
I Know I need to use regex to retrieve f6 in this instance, however, I am unsure on the expression. From a brief tutorial on regex, I've tried using the code below to achieve this with no luck

`Regex.Match(output, @"=\w*,").Value.Replace("\"", "");`


Any help would be appreciated.

Answer Source

You can use this pattern:

"=\\s([A-Za-z0-9]{2}),"
//or
"=\\s(\\w+),"

Code:

string str = "stop = F6, quantity ( 1 )  ";
var res = Regex.Matches(str, "=\\s([A-Za-z0-9]{2}),")[0].Groups[1].Value;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download