+ indep. WoS citations

Python and Networks -- Pattern test -- 2016-04-04

1.  Python code (pypatt.py)

import re

# the list of strings
strList = ['abb','abc']

# the list of patterns
patternList = [r'..',r'.*',r'.*?',r'^.*b',r'^.*?b',r'(\w)+',r'(\w)\1*',r'(.)\1',r'(?:.)(.)']

# check match for all strings and patterns
print("")
print("string  pattern   match(es)")
print("")
for string in strList:
    for pattern in patternList:
        print ( "%-8s%-10s[%s]" %
                ( string, pattern,
                  ', '.join(["'"+_+"'" for _ in re.findall(pattern,string)])
                )
              )
    print("")

2.  Usage and output

$ python3 pypatt.py

string  pattern   match(es)

abb     ..        ['ab']
abb     .*        ['abb', '']
abb     .*?       ['', '', '', '']
abb     ^.*b      ['abb']
abb     ^.*?b     ['ab']
abb     (\w)+     ['b']
abb     (\w)\1*   ['a', 'b']
abb     (.)\1     ['b']
abb     (?:.)(.)  ['b']

abc     ..        ['ab']
abc     .*        ['abc', '']
abc     .*?       ['', '', '', '']
abc     ^.*b      ['ab']
abc     ^.*?b     ['ab']
abc     (\w)+     ['c']
abc     (\w)\1*   ['a', 'b', 'c']
abc     (.)\1     []
abc     (?:.)(.)  ['b']