2

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.

0

3 Answers 3

6

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

Working Demo

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

Comments

1
string thestring = @"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";

string matchedString = Regex.Match(thestring,@"COM[\d]+").Value;

The Regex that is matched against the string (COM[\d]+) means:

match an instance of COM followed by at least one instance (+) of a digit (\d)

This is assuming there is only one instance of COM(NUMBER) in your string.

You can also put a space to make sure that only space COM is matched by the Regex, like so:

string matchedString = Regex.Match(thestring,@"\sCOM[\d]+").Value;

1 Comment

use \b before COM to match only COM instead of XCOM
1

You can use a regex like this:

\b(COM\d+)\b

Working demo

enter image description here

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.