Extract a word from string using regex

phantomsays

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.

hwnd

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Extract word from string Using python regex

From Dev

Using regex to extract numbers and symbols from string

From Dev

Using regex to extract information from string

From Dev

Extract a string from textfile using JS regex

From Dev

Extract ip address from a string using regex

From Dev

Python - Extract pattern from string using RegEx

From Dev

Extract values from a string using regex

From Dev

Extract phone numbers from string using regex?

From Dev

How to extract numbers from a string using regex?

From Dev

Extract string from URL using Regex

From Dev

Extract specific chars from a string using a regex

From Dev

extract url from string using regex

From Dev

How to extract substring from a string using regex?

From Dev

Extract values from string using regex groups

From Dev

java: extract string from input using regex

From Dev

Using regex to extract an image url from string

From Dev

Using regex to extract data from the following string

From Dev

Extract version from string using java regex

From Dev

extract information from string using regex in R

From Dev

Extract text using word VBA regex then save to a variable as string

From Dev

Extract word from specified string with regex delimited by tab

From Dev

Extract word from specified string with regex delimited by tab

From Dev

Extract string from regex

From Dev

Extract word from string using grep/sed/awk

From Dev

Scikit Learn - Extract word tokens from a string delimiter using CountVectorizer

From Dev

extract part of word into a field from a long string using R

From Dev

Extract word from string using grep/sed/awk

From Dev

Regex: extract String from String

From Dev

Regex: extract String from String

Related Related

HotTag

Archive