Check if specific word appears before other word using regex
Question:
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?
Answers:
You need to negate the name
before Jack
.
(?=^(?:(?!name).)*Jack.*$).*
Short Explanation
(?:(?!name).)*
Anything except the word name
Jack.*
Exact word Jack
and 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
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):
^(?:Jackb|(?=w+(?: +(?!nameb)w+)* +Jackb)).*
This regex uses alternation to either:
- Match
Jack
at the start OR
- Match
Jack
before matching 1+ words if that word is not full word name
.
Now if you want to disallow full word name
anywhere in the line i.e before or after then use:
^(?!.*bnameb).*bJackb.*
To check if there is no name
before Jack
how about
^W*(?:(?!nameb)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).
One can avoid regex and just use string index method.
s = 'My name is Jack Brown'
s.index('name') < s.index('Jack')
True
If one needs regex, the same indexing idea could be used as follows:
def check_string(s,name='nam', Jack='Jac'):
try:
return re.search(rf'b{name}.*?b', s).start() < re.search(rf'b{Jack}.*?b', s).start()
except Exception as e:
return False
s = 'My is Jack Brown'
check_string(s)
False
s = 'My name is Jack Brown'
check_string(s)
True
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?
You need to negate the name
before Jack
.
(?=^(?:(?!name).)*Jack.*$).*
Short Explanation
(?:(?!name).)*
Anything except the wordname
Jack.*
Exact wordJack
and 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
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):
^(?:Jackb|(?=w+(?: +(?!nameb)w+)* +Jackb)).*
This regex uses alternation to either:
- Match
Jack
at the start OR - Match
Jack
before 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:
^(?!.*bnameb).*bJackb.*
To check if there is no name
before Jack
how about
^W*(?:(?!nameb)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).
One can avoid regex and just use string index method.
s = 'My name is Jack Brown'
s.index('name') < s.index('Jack')
True
If one needs regex, the same indexing idea could be used as follows:
def check_string(s,name='nam', Jack='Jac'):
try:
return re.search(rf'b{name}.*?b', s).start() < re.search(rf'b{Jack}.*?b', s).start()
except Exception as e:
return False
s = 'My is Jack Brown'
check_string(s)
False
s = 'My name is Jack Brown'
check_string(s)
True