<jupyter_start><jupyter_text>Regular Expressions in Python A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern.A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression.Regular expressions can contain both special and ordinary characters. Most ordinary characters, like 'A', 'a', or '0', are the simplest regular expressions; they simply match themselves.Regular expressions can contain both special and ordinary characters. Most ordinary characters, like 'A', 'a', or '0', are the simplest regular expressions; they simply match themselves. The **Match** function attempts to match RE pattern to string with optional flags.**Syntax:**re.match(pattern, string, flags=0) where,pattern - regular expression to be matched.string - string, which would be searched to match the pattern at the beginning of string.flags - different modifiers  Module contents**1. re.compile(pattern, flags=0)**Compile a regular expression pattern into a regular expression object, which can be used for matching using its match() and search() methods**2. re.search(pattern, string, flags=0)**Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.**3. re.match(pattern, string, flags=0)**If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match.**4. re.fullmatch(pattern, string, flags=0)**If the whole string matches the regular expression pattern, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match.**5. split(pattern, string, maxsplit=0, flags=0)**Split string by the occurrences of pattern. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the final element of the list.<jupyter_code>re.split('\W+', 'Words, words, words.')<jupyter_output><empty_output><jupyter_text>Match ObjectsMatch objects always have a boolean value of True. Since match() and search() return None when there is no match, you can test whether there was a match with a simple if statement<jupyter_code>import re
match = re.search(pattern, string)
if match:
    process(match)<jupyter_output><empty_output><jupyter_text>match.group([group1, ...])-  Returns one or more subgroups of the match.-  If there is a single argument, the result is a single string; -  if there are multiple arguments, the result is a tuple with one item per argument. -  Without arguments, group1 defaults to zero (the whole match is returned).<jupyter_code>m = re.match(r"(\w+) (\w+)", "<NAME>, physicist")
m.group(0)        # prints, '<NAME>'

m.group(1)       # The first parenthesized subgroup. prints, 'Isaac'

m.group(2)       # The second parenthesized subgroup. prints, 'Newton'

m.group(1, 2)    # Multiple arguments give us a tuple.
m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "<NAME>")
m.group('first_name')
#m.group('last_name')
#Named groups can also be referred to by their index
m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "<NAME>")
m.group(1)
#m.group(2)<jupyter_output><empty_output><jupyter_text>Matching Versus SearchingPython offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string<jupyter_code>import re

line = "Cats are smarter than dogs";

matchObj = re.match( r'dogs', line, re.M|re.I)
if matchObj:
   print "match --> matchObj.group() : ", matchObj.group()
else:
   print "No match!!"

searchObj = re.search( r'dogs', line, re.M|re.I)
if searchObj:
   print "search --> searchObj.group() : ", searchObj.group()
else:
   print "Nothing found!!"<jupyter_output>No match!!
search --> searchObj.group() :  dogs<jupyter_text>Search and ReplaceOne of the most important re methods that use regular expressions is sub.**Syntax:**re.sub(pattern, repl, string, max=0)This method replaces all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided. This method returns modified string.<jupyter_code>import re

phone = "2004-959-559 # This is Phone Number"

# Delete Python-style comments
num = re.sub(r'#.*$', "", phone)
print "Phone Num : ", num

# Remove anything other than digits
num = re.sub(r'\D', "", phone)    
print "Phone Num : ", num<jupyter_output>Phone Num :  2004-959-559 
Phone Num :  2004959559
{'fraction_non_alphanumeric': 0.09432108469247397, 'fraction_numerical': 0.010611122027903321, 'mean_word_length': 5.009445100354191, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '123294', 'license': 'MIT', 'n_tokens_mistral': 1406, 'n_tokens_neox': 1343, 'n_words': 648, 'path': 'Homeworks/Homework 2/PURVACHAR_HW2_REGEX_FALL2018.ipynb', 'repo_name': 'RashmiPurvachar/big-data-python-class', 'size': '5121'}