Michal Shilo
Michal Shilo

Reputation: 23

How to search for at least one of two groups in Python Regex, when also looking for a third group that is a must?

I'm trying to use regex in order to find missused operators in my program.

Specifically I'm trying to find whether some operators (say %, $ and @) were used without digits flanking their both sides.

Here are some examples of missuse:

'5%'
'%5'
'5%+3'
'5%%'

Is there a way to to that with a single re.search?

I know I can use + for at least one, or * for at least zero, but looking at:

([^\d]*)(%)([^\d]\*)

I would like to find cases where at least one of group(1) and group(3) exist,
since inserting % with digits on its both sides is a good use of the operator.

I know I could use:

match = re.search(r'[^\d\.]+[@$%]', user_request)
if match: 
    return 'Illegal use of match.group()'

match = re.search(r'[@$%][^\d\.]+', user_request)
if match: 
    return 'Illegal use of match.group()'

But I would prefer to do so with a single re.search line.

And also - when I use [^\d.] does this include the beginning the end of the string? Or only different chars?

Thank you :)

Upvotes: 1

Views: 391

Answers (1)

The fourth bird
The fourth bird

Reputation: 163362

You might use an alternation with a negative lookahead and a negative lookbehind to assert what is before and what is after is not a digit:

(?<!\d)[@$%]|[@$%](?!\d)

That will match:

  • (?<!\d) Negative lookbehind to check what is on the left is not a digit
  • [@$%] Character class, match one of @, $ or %
  • | Or
  • [@$%] Character class, match one of @, $ or %
  • (?!\d) Negative lookahead to check what is on the right is not a digit

For example:

match = re.search(r'(?<!\d)[@$%]|[@$%](?!\d)', user_request)
if match: 
    return 'Illegal use of match.group()'

Regex demo | Python demo

[^\d.] Matches not a digit or a literal dot. The ^ inside a character class negates what it contains. But if it is the first character of the string that is not a digit or a dot then it will match.

Upvotes: 2

Related Questions