I have c# code in which I run a perl file using commandline and capture that output in a c# string. I want to extract a certain word from this string using regex. I tried several methods to capture that specific word but it didnt work.
For example: the below text was captured in a string in c#
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
Initializing.
jsdns jsdnjs wuee uwoqw duwhduwd 9-8 is = COM10
uuwe sodks asjnjx
In the above code I want to extract COM10. Similarly this value can also change to COM12 or COM8 or COM15. I will always have COM in the text but the succeeding number can change.
Can someone let me know how to go about with regex. I used RegexOptions.Multiline but am not sure how to go about it. Also if an explanation is included it would be helpful.
You can use the following regex.
Match m = Regex.Match(input, @"\b(?i:com\d+)");
if (m.Success)
Console.WriteLine(m.Value); //=> "COM10"
Explanation:
\b # the boundary between a word character (\w) and not a word character
(?i: # group, but do not capture (case-insensitive)
com # 'com'
\d+ # digits (0-9) (1 or more times)
) # end of grouping
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments