Python Regular Expressions Examples.

Python regex examples.

  1. Bhawani Singh
    Python At A Glance

    Python is an effective and object-oriented terminology, commonly used for web database integration. 90% of people choose Python over other technology because of its convenience, stability and simple interfacing. Python follows step-by-step and object-oriented development paradigms and hence, the different programs coded in Python come out with the clean and understandable rule, making them simple to sustain.

    Python Regular Expressions

    A regular expression is a unique series of figures that assist you to find other strings, using a specific format kept in a pattern. Regular expressions are very crucial and mostly used in UNIX world.

    Now we are learning about regular expressions in python which is is appropriately named “re”.

    >>> import re

    7 Python Regular Expressions Examples – Re Match Search Fi

    1. Python with Raw Strings

    1. If the backslash is followed by a unique series identified by the parser, the whole escape series is changed by a corresponding unique character.

    2. This action causes an issue when operating with frequent expression in python because the ‘re’ program also uses backslash figures to escape unique regex figures.

    3. A raw series or string is designed through including an ‘r’ character before quotation of a normal string.

    >>> str = 'This is a\simple string sequence'

    >>> rawStr = r'and this is a\other string'

    >>> print str

    This is a

    string sequence

    >>> print rawStr

    and this is a\other string


    See Result With Python Regex

    The ‘re’ provides the kind of methods to perform many queries on given input string. Just see the methods.

    re.match()

    re.search()

    re.findall()

    Every method needs a regular expression and string to scan for find.

    2. Query Using re.match – Matches from start

    >>> re.match(r'sun', 'sun mon sun')

    <_sre.SRE_Match object at 0xb743e720<

    >>> match = re.match(r'sun', 'sun mon sun')

    >>> match.group(0)

    'sun'

    if we use match() on the same string, and here we are looking for the pattern ‘mon’, we will not:

    >>> re.match(r'mon', 'sun mon sun')

    >>>

    3. Query Using re.search – Matches start From Anywhere

    It is similar to match(), but here search() is not restrict to finding matches at the starting point of the string, so here if we are searching for ‘mon’ so result will be:

    search(r'mon', 'sun mon sun')

    >>> match.group(0)

    'mon'

    If it has found, it stops looking after a match, so here search() for ‘sun’ in our given string only finds the first:

    >>> match = re.search(r'sun', 'sun mon sun')

    >>> match.group(0)

    'sun'

    4. See the Result Using re.findall – Find All Matching

    This is to get all list maches, when you call findall(), you will simply get a list of all matching patterns. See the result:

    >>> re.findall(r'sun', 'sun mon sun')

    ['sun', 'sun']

    >>> re.findall(r'mon', 'sun mon sun')

    ['mon']

    5. Get The Result Using match.start and match.end Methods

    In given example, the match object generate the start and end indexes of the matching list from the give original string:

    >>> match = re.search(r'sun', 'sun mon sun')

    >>> match.start()

    0

    >>> match.end()

    3

    This is very useful information sometimes.

    6. Group by Number With match.group Method

    With the good facility of match.group Method, it is easy to group by number. Let’s see given example to know how does it work:

    >>> contInfo = 'Shail, Mah: 1625-2016'

    Here you can match with a regular expression and see the result:

    >>> re.search(r'\w+, \w+: \S+', contInfo)

    <_sre.SRE_Match object at 0xb74e1ad8<

    Here you may group the content and work with individual groups.

    >>> match = re.search(r'(\w+), (\w+): (\S+)', contInfo)

    You can have this group using the match object’s group() method.

    >>> match.group(1)

    'Shail'

    >>> match.group(2)

    'Mah'

    >>> match.group(3)

    '1625-2016'

    Here indexing start with 1 is because group 0 is reserved for holding the entire match.

    >>> match.group(0)

    'Shail, Mah: 1625-2016'

    7. One Another match.group Method For Grouping by Name

    Python is widely used language and it also allows you to work with Grouping by Name using the following given syntax:

    >>> match = re.search(r'(?P<fname>\w+), (?P<lname>\w+): (?P<contact>\S+)', contInfo)

    Here we can also have the grouped content using the group() method, but here need to specify the name:

    >>> match.group('lname')

    'Shail'

    >>> match.group('fname')

    'Mah'

    >>> match.group('contact')

    '1625-2016'

    This can also used with the findall () method too, you may perform this with the findall (), this will return a list. See the regex pattern:

    >>> re.findall(r'(\w+), (\w+): (\S+)', contInfo)

    [('Shail', 'Mah', '1625-2016')]

    Named grouping is not working when using the findall() method.