Reputation: 23
I trying to match the string ARIBABA only if .get doesn't comes before it.
Example:
ARIBABA = config.get('SOMETHING', 'ARIBABA').lower()
I've tried this (down below) but it just doesn't match anything.
^(.get)\bARIBABA\b
Upvotes: 0
Views: 100
Reputation: 18611
You must use PyPi regex library here:
import regex
s = "ARIBABA = config.get('SOMETHING', 'ARIBABA').lower()"
p = r"(?<!\.get\b.*)\bARIBABA\b"
print(regex.findall(p, s))
See Python proof.
EXPLANATION
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
get 'get'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
ARIBABA 'ARIBABA'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
Upvotes: -1
Reputation: 644
Here is pure regex solution.
pattern = "^(?!.*\.get).*(ARIBABA)"
aribaba = "config.get('SOMETHING', 'ARIBABA').lower()"
try:
re.search(pattern, aribaba).group(1)
except:
print("No ARIBABA or get comes before")
Upvotes: -1
Reputation: 1658
You can simply try this one:
if 0 =< s.find('.get') < s.find('ARIBABA'):
s.find(substring)
returns the lowest index of s that begins with the substring
Full example:
s = "config.get('SOMETHING', 'ARIBABA').lower()"
if 0 =< s.find('.get') < s.find('ARIBABA'):
print('.get comes before ARIBABA')
Output:
.get comes before ARIBABA
EDIT:
if the substring doesn't exist in s, find will return -1, that's why I added the 0=<
condition
Upvotes: 5