I want to check if specific word appears before another word? If it appears it must not match, if doesn't appear it must match, there can be other words between them
Here is example string: My name is Jack Brown
I want to check if name word appears before Jack, here is my regex:
regex: (?<!name)\b(Jack)\b
but untortunately it works only when there is no any words between them, can someone help?
CodePudding user response:
You need to negate the name before Jack.
(?=^(?:(?!name).)*Jack.*$).*
Short Explanation
(?:(?!name).)*Anything except the wordnameJack.*Exact wordJackand other optional characters.*Match the whole string if assertions are true
Python Example
import re
s1 = "My name is Jack Brown"
s2 = "I am Jack Brown"
def doesMatch(string):
return bool(re.search(r"(?=^(?:(?!name).)*Jack.*$).*", string))
print(doesMatch(s1)) # False
print(doesMatch(s2)) # True
Also, see the regex demo
CodePudding user response:
If you want to disallow full word name before matching full word Jack then this regex will be much more efficient (take note of number of steps on regex101 site):
^(?:Jack\b|(?=\w (?: (?!name\b)\w )* Jack\b)).*
This regex uses alternation to either:
- Match
Jackat the start OR - Match
Jackbefore matching 1 words if that word is not full wordname.
Now if you want to disallow full word name anywhere in the line i.e before or after then use:
^(?!.*\bname\b).*\bJack\b.*
CodePudding user response:
To check if there is no name before Jack how about
^\W*(?:(?!name\b)\w \W )*?Jack.*
This pattern checks if none of the words before jack is name by use of a negative lookahead.
\w matches a word character and upper \W the negation of it (non word characters).
CodePudding user response:
One can avoid regex and just use string index method.
s = 'My name is Jack Brown'
s.index('name') < s.index('Jack')
True
