Python Tutorial for Beginners

Python RegEx Sets Explained

What Are Sets in Python RegEx?

A set is a group of characters having a specific meaning enclosed by a pair of square brackets []. The sets are iterable and mutable, with no presence of duplicate elements. 

The purpose of using Python sets in place of lists is that these can check the availability of particular elements in the set in an optimum manner. However, because of the unordered nature of data types in sets, you can’t use indexes to access items. 

  • [arn]: Returns a match that contains one of the supplied characters (a, r, or n).

  • [a-n]: Returns a match for any lower case character between the letters a and n in alphabetical order.

  • [^arn]: EXCEPT for a, r, and n, this function returns a match for any character.

  • [0123]: Returns a match if any of the required numbers are present (0, 1, 2, or 3).

  • [0-9]: It returns a match for any number in the range of 0 to 9.

  • [0-5][0-9]: Any two-digit number between 00 and 59 returns a match.

  • [a-zA-Z]: Returns a match for any character between a and z, lower case OR upper case, alphabetically.

[+]: +, *,., |, (), $, have no special meaning in sets, so [+] means: find a match for any + character in the string.


Did you find this article helpful?