Python re compile-character classes-\d














































Python re compile-character classes-\d



\d is a character class that represents a digit. This is used in the regular expression to extract one digit. They can be clubbed together to find groups just like any other regular expression pattern.


Let us take an example.

CODE :

import re Regex_Object = re.compile(r'\d\d') mo1 = Regex_Object.search('My roll number is 07') print('EXTRACTED ROLL NUMBER FROM THE TEXT IS '+mo1.group())



OUTPUT :

EXTRACTED ROLL NUMBER FROM THE TEXT IS 07



In the example the number 07 is extracted since the regex object matches a 2 digit number pattern.


Comments