Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 regular expression for $ but not $$ in a string

I need to match one of the following anywhere in a string:

${aa:bb[99]}
${aa:bb}
${aa}

but not:

$${aa:bb[99]}
$${aa:bb}
$${aa}

my python 3 regex is:

pattern = **r"[^\$|/^]**\$\{(?P<section>[a-zA-Z]+?\:)?(?P<key>[a-zA-Z]+?)(?P<value>\[[0-9]+\])?\}"

What I'm looking for, is the proper way to say not $ or beginning of a string. The block r"[^\$|/^]" will properly detect all cases but will fail if my string starts at the first character.

I trie, without success:

r"[^\$|\b]... 
r"[^\$|\B]...
r"[^\$]...
r"[^\$|^] 

Any suggestion?

like image 333
Michel Rondeau Avatar asked Dec 29 '17 20:12

Michel Rondeau


People also ask

What does ?= Mean in regular expression?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

How do you check if a regex is in a string Python?

Practical Data Science using Python For checking if a string consists only of alphanumerics using module regular expression or regex, we can call the re. match(regex, string) using the regex: "^[a-zA-Z0-9]+$". re. match returns an object, to check if it exists or not, we need to convert it to a boolean using bool().

What does \W mean in Python?

The meta-characters which do not match themselves because they have special meanings are: . ^ $ * + ? { [ ] \ | ( ) (details below) . (a period) -- matches any single character except newline '\n' \w -- (lowercase w) matches a "word" character: a letter or digit or underbar [a-zA-Z0-9_].


1 Answers

Use a negative lookbehind:

(?<!\$)

and then follow it by the thing you actually want to match. This will ensure that the thing you actually want to match is not preceded by a $ (i.e. not preceded by a match for \$):

(?<!\$)\$\{(?P<section>[a-zA-Z]+?\:)?(?P<key>[a-zA-Z]+?)(?P<value>\[[0-9]+\])?\}
     ^  ^
     |  |
     |  +--- The dollar sign you actually want to match
     |
     +--- The possible second preceding dollar sign you want to exclude

(?<!...)

Matches if the current position in the string is not preceded by a match for .... This is called a negative lookbehind assertion. Similar to positive lookbehind assertions, the contained pattern must only match strings of some fixed length and shouldn’t contain group references. Patterns which start with negative lookbehind assertions may match at the beginning of the string being searched.

https://docs.python.org/3/library/re.html

like image 135
Amber Avatar answered Sep 18 '22 15:09

Amber