/  Python – Regular Exp

Python – Regular Exp

Python – Regular Exp:

Regular Expressions can be used for searching a word or a character or digits from the given data and several patterns. These can be called as RREs, regex patterns.

We just need to import the module “re” to work with regular expressions.

 Regular expression syntax:

\d – Matches any decimal digit [0-9].

\D – Matches any non-digit character [^0-9].

\s – Matches any whitespace character [ \t\n\r\f\v].

\S – Matches any non-whitespace character [^ \t\n\r\f\v].

\w – Matches any alphanumeric character [a-zA-Z0-9_].

\W – Matches any non-alphanumeric character [^a-zA-Z0-9_].

Handling White Spaces:

\n = Used for new line

\s = Used for space

\t = Used for tab

\e = Used for escape

\f = Used for form feed

\r = Used for carriage return

Let us see the below example which uses the find all method to get the search result of salary from the data given as input to techdata variable.


import re

>>>techdata = '''

... working on design technologies gives you $10000 salary.

... working on programming technologies gives you $15000 salary.

... working on Latest technologies give you $20000 salary.

... '''

>>> salary = re.findall(r'\d{1,10}',techdata)

Let us print the search result from data.

>>> print(salary)
['10000', '15000', '20000']