dgnin
dgnin

Reputation: 1597

Exclude sequences of characters (not only single characters) inside a regular expression in Python

I have expressions with this form...

@@name<·parameters·>

...and I want a regular expression that matches the groups name and parameters. As I have a closed (and small) group of values for name I preffer to use a for loop to try with all the few values, but parameters can be anything... anything except and ·>, wich are the sequences for opening and closing sets of parameters.

I found this question and I tried this...

@@(name)<·((?!(<·|·>).*))·>

...but I can't get it working. I think that the reason is that there the excluded expression is known in position and in number of repetitions (1) but in my case I want to exclude every occurrence of any of this two sequences in a string of unknown length.

Do you know how to do it? Thank you.

Upvotes: 2

Views: 858

Answers (2)

Mayur Koshti
Mayur Koshti

Reputation: 1862

You can also use the following regex:

@@([^<]*)<\·([^\·]+)\·>

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174776

You regex must be,

@@(name)<·((?:(?!<·|·>).)*)·>

Negative lookahead method. Just understand this part (?!<·|·>). only which matches any character(dot) but not of or ·> , (?:(?!<·|·>).)* zero (star) or more times.

or

Non-greedy method.

@@(name)<·(.*?)·>

DEMO

Upvotes: 2

Related Questions