Home > database >  Match end of line of line if line contains specific text
Match end of line of line if line contains specific text

Time:09-27

I doing trying to extract words from an end of a line if a line contains a specific phrase using RegEx.
For example I have these lines:

Web Mode .............................. Enable
Secure Web Mode.............................Enable
AP Fallback ................................ Enable

And I would like to find the lines starting with "Web Mode" for example and if the line starts with "Web Mode" I would like to match the word at the end of the line which in this case is "Enable".

I have been able to match the beginning of the line like so:

^Web Mode.*

but I'm stuck at this point, any Ideas?

CodePudding user response:

For the example data, you could use a capture group to match Enable

^Web Mode \.  (\S )$

The pattern matches:

  • ^ Start of string
  • Web Mode Match literally followed by a space
  • \. Match 1 or more occurrences of a dot
  • (\S ) Match a space char capture 1 non whitespace chars in group 1
  • $ End of string

See a regex demo

CodePudding user response:

Use

^Web Mode.*\b\K\w $

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  Web Mode                 'Web Mode'
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  \K                       match reset operator
--------------------------------------------------------------------------------
  \w                       word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
  • Related