Reputation: 1003
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?
Upvotes: 1
Views: 1564
Reputation: 18611
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
Upvotes: 1
Reputation: 163362
For the example data, you could use a capture group to match Enable
^Web Mode \.+ (\S+)$
The pattern matches:
^
Start of stringWeb 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 stringSee a regex demo
Upvotes: 3