Wednesday, May 4, 2016

Python Extension Notations (Regular Expressions)

Extension Notations

Extension notations start with a (? but the ? does not mean o or 1 occurrence in this context. Instead, it means that you are looking at an extension notation. The parentheses usually represents grouping but if you see (? then instead of thinking about grouping, you need to understand that you are looking at an extension notation.

Unpacking an Extension Notation

example: (?=.com)

step1: Notice (? as this indicates you have an extension notation

step2: The very next symbol tells you what type of extension notation.

(?=       Only do a match if what follows the equal sign follows

(?#        Indicates a comment

(?!        Only do a match if what follows the ! does not follow in the string

(?iLmsux)        This turns on in python some special flags. I will elaborate below.

step3: How to use these. As far as I can tell, these are specific to python's regular expression engine. I have not found many references to them elsewhere and in the case of (?iLmsux) these are python regex specific flags, so I am thinking these are only implemented in python's regex engine.

inputString = r"genericWebSite.com"
regExString = r'(?=.com)'
m = re.search(regExString, inputString)

if m is not None: # m is a match object    print(m.group())
    print(type(m))
else:
    print('m is None, so no match was found')


Running this code returns a match object but m.group() does not return the match. 


No comments: