Home > Blockchain >  Check if specific word appears before other word using regex
Check if specific word appears before other word using regex

Time:08-08

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 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

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 Jack at the start OR
  • Match Jack before matching 1 words if that word is not full word name.

RegEx Demo 1

Now if you want to disallow full word name anywhere in the line i.e before or after then use:

^(?!.*\bname\b).*\bJack\b.*

RegEx Demo 2

CodePudding user response:

To check if there is no name before Jack how about

^\W*(?:(?!name\b)\w \W )*?Jack.*

Here is a demo at regex101

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
  • Related