Reputation: 53
I want to match nvalue if it's NOT the only string on a line.
nvalue : should not match
This nvalue : should match
Nvalue example : should match
I know the regex to match nvalue if it's the only string:
^\bnvalue\b$
but I don't know how to turn it around.
Upvotes: 1
Views: 88
Reputation: 23914
You can use match-group and operator |
as alternation to match both possibilities:
((\w+ nvalue)|(nvalue \w+)
)and it works on all regex-search-engine
with flags g
: global
............... i
: case-sensitive
................m
: multi-line
or:
((\w+ [Na]value)|([Na]value \w+))
Upvotes: 1
Reputation: 9650
Use negative lookbehind:
^(?!nvalue$).*nvalue
Here ^(?!nvalue$)
means match beginning of a string only is it is not followed by nvalue
and end of string $
immediately after that.
Demo: https://regex101.com/r/1vOhQt/1
Update
If you need to extract the nvalue
only, wrap it into a capture group using parenthesis:
^(?!nvalue$).*(nvalue)
The result will be stored in the first match group. Demo: https://regex101.com/r/1vOhQt/3.
If you're using PCRE flavour, use \K
to reset any previously matched subpattern:
^(?!nvalue$).*\Knvalue
Demo: https://regex101.com/r/1vOhQt/2
In Python the \K
is supported in regex
module.
Upvotes: 2