Skip to content Skip to sidebar Skip to footer

Regex Python Conditions

I coded an app with tkinter. Now I want to add conditions/pattern in a entry box when the user use it. I want to force the user to use these pattern: The entry must begin by this p

Solution 1:

Note that [A-I] does not match the Q in Q7. You can use [A-Z] to extend the range and use a repeating group for the first and the second pattern.

You might use

#\d+\s[A-Z]\d+(?:[,-][A-Z]\d+)*(?:;#\d+\s[A-Z]\d+(?:[,-][A-Z]\d+)*)*

The pattern matches:

  • #\d+\s[A-Z]\d+ Match # 1+ digits, a whitespace char and 1+ digits
  • (?:[,-][A-Z]\d+)* Optionally repeat matching , or - char A-Z and 1+ digits
  • (?: Non capture group
    • ;#\d+\s[A-Z]\d+ Match ; and the first pattern
    • (?:[,-][A-Z]\d+)* Optionally repeat matching the second pattern
  • )* Close non capture group and optionally repeat the whole part

Regex demo

Or if you want to allow spaces as well:

#\d+\s[A-Z]\d+(?:[,-]\s?[A-Z]\d+)*(?:;#\d+\s[A-Z]\d+(?:[,-]\s?[A-Z]\d+)*)*

Regex demo

Post a Comment for "Regex Python Conditions"