Extract entire line containing keyword

Hello

I have a string variable containing many lines of text, and I wold like to extract an entire line which contains a keyword.

For example, if my string is:

-66.48 ml End Phase (Issued) (Processing) (Completed)
-66.48 ml Phase Equilibration (Issued) (Processing) (Completed)
-66.48 ml Base SameAsMain
-66.48 ml Set mark “Equilibration” (Issued) (Processing) (Completed)
-66.48 ml Inlet A A1 (Issued) (Processing)
-66.46 ml Inlet A A1 (Completed)
-66.46 ml Inlet B B1 (Issued) (Processing)

I would like to extract the entire line which contains the text “SameAsMain”. So my output should be:

-66.48 ml Base SameAsMain

Can someone share how to do this?

Note: I am only showing a few lines of my variable here, my entire variable has a lot of lines.

Hi,

If there is single target line in your string, the following will work.

System.Text.RegularExpressions.Regex.Match(yourString,"^.*SameAsMain.*$",System.Text.RegularExpressions.RegexOptions.Multiline).Value

If there are muliple target lines and get all the lines, the follwoing will work.

String.Join(vbCrLf,System.Text.RegularExpressions.Regex.Matches(yourString,"^.*SameAsMain.*$",System.Text.RegularExpressions.RegexOptions.Multiline).Cast(Of System.Text.RegularExpressions.Match).Select(Function(m) m.Value))

Regards,

1 Like

image

  1. assign this to array of string e.g. lines, where txt is the entire text
    lines = txt.Split({vbCrLf, vbCr, vbLf}, StringSplitOptions.None)

  2. then this will give you your line
    lines.FirstOrDefault(Function(x) x.Contains("KEYWORD"))

1 Like

That works, thanks a lot!

1 Like

This solution works perfect too. Thank you very much!

1 Like

Main.xaml (7.7 KB)
You can check this also

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.