<jupyter_start><jupyter_text>Regular Expression Quick Guide^        Matches the beginning of the line$        Matches the end of the line.        Matches any character\s       Matches whitespace\S       Matches any non-whitespace character*        Repeats a character zero or more times*?       Repeats a character zero or more times (non-greedy)+        Repeats a character one or more times+?       Repeats a character one or more times (non-greedy)[aeiou]  Matches a single character in the listed set[^XYZ]   Matches a single character not in the listed set[a-z0-9] The set of characters can include a range(        Indicates where string extraction is to start)        Indicates where string extraction is to end{}[]\d any digit\D anything except for a digit\w equals to [a-zA-Z0-9_]\W equals to [^a-zA-Z0-9_]<jupyter_code>Class Recording 8 from 56:13. Edureka<jupyter_output><empty_output><jupyter_text>A character can be a-z, A-Z, 0-9, special chars(~!@$%^&*), spaces  hare = open('C:\\Users\\HP\\Desktop\\Sudheer DS\\LearnDataScience-master\\Python_Handouts\\hare.txt','w+')hare.write("This notepad file is created using Anaconda")hare.write(" This is a great day")hare.seek(0)print(hare.read())hare.close()<jupyter_code>#               find(), re.search(),  startswith()<jupyter_output><empty_output><jupyter_text>Using re.search() like find()hand = open('mbox-short.txt')for line in hand:    line = line.rstrip()    if line.find('From:') >= 0:        print(line) Using re.search() like find()import rehand = open('mbox-short.txt')for line in hand:    line = line.rstrip()    if re.search('From:',line):        print(line)  Using re.search() like startswith()hand = open('mbox-short.txt')for line in hand:    line = line.rstrip()    if line.startswith('From:'):        print(line)  Using re.search() like startswith() Ignoring import re and came to know that it is not necessary to call on the library everytime.hand = open('mbox-short.txt')for line in hand:    line = line.rstrip()    if re.search('^From:',line):   Observe the previous codes ^ in not necessary also here.        print(line)  Wild-card charactersimport rehand = open('rexp.txt')for line in hand:    line = line.rstrip()if re.search('^X.*:', line) :    print(line)  Fine tuning your Matchhand = open('rexp.txt')for line in hand:    line = line.rstrip()if re.search('^X-\S+:', line) :    print(line)  Matching and Extracting Dataimport rex = 'My 2 favorite numbers are 19 and 42'y = re.findall('[0-9]+',x)print(y) import rex = 'My 2 favorite numbers are 19 and 42'y = re.findall('[0-9]+',x)                   re.findall always returns a listprint(y)  Experimentimport rex = 'My 2 favorite numbers are 19 and 42'y = re.findall('[0-9]',x)                   re.findall always returns a listprint(y)  Experimentimport rex = 'My 2 favorite numbers are 19 and 42'y = re.findall('[0-9]*',x)                   re.findall always returns a listprint(y)  Experiment - It Workedimport rex = 'My 2 favorite numbers are 19 and 42'y = re.findall('[^A-z\s]+',x)                   re.findall always returns a listprint(y) y = re.findall('[AEIOU]+',x)print(y)   Experimenty = re.findall('[AEIOUM]+',x)print(y)  Experimentx = 'My 2 favorite numbers are 19 and 42'y = re.findall('[A-z]+',x)print(y)  Warning: Greedy Matchingimport rex = 'From: Using the : character'y = re.findall('^F.+:', x)         the last colon of the stringprint(y)  Non-Greedy Matchingimport rex = 'From: Using the : character'y = re.findall('^F.+?:',x)          the first colon of the stringprint(y)  Fine-Tuning String Extractionx = 'From <EMAIL> Sat Jan  5 09:14:16 2008'y= re.findall('\S+@\S+',x)print(y)  Fine-Tuning String Extractionx = 'From <EMAIL> Sat Jan  5 09:14:16 2008'y= re.findall('\S+@\S+',x)print(y)y = re.findall('^From:.*? (\S+@\S+)',x)print(y) data = 'From <EMAIL> Sat Jan 5 09:14:16 2008'atpos = data.find('.')print(atpos) data = 'From <EMAIL> Sat Jan 5 09:14:16 2008'atpos = data.find('o')print(atpos) data = 'From <EMAIL> Sat Jan 5 09:14:16 2008'atpos = data.find('@')print(atpos)  Denotes space which comes after atpos - here it is "@"sppos = data.find(' ',atpos)print(sppos) host = data[atpos+1 : sppos]print(host)  The Double Split Patternline = 'From <EMAIL> Sat Jan 5 09:14:16 2008'words = line.split()words email = words[1]email pieces = email.split('@')pieces print(pieces[1])  The Regex Version        import relin = 'From <EMAIL> Sat Jan 5 09:14:16 2008'y = re.findall('@([^ ]*)',lin)        [^ ] Match non blank character    [^ ]* Match non blank character Match many of themprint(y)  Experimentimport relin = 'From <EMAIL> Sat Jan 5 09:14:16 2008'y = re.findall('@([^ ]+)',lin)        [^ ] Match non blank character    [^ ]* Match non blank character Match many of themprint(y)  Even Cooler Regex Versionimport relin = 'From <EMAIL> Sat Jan 5 09:14:16 2008'y = re.findall('^From .*@([^ ]*)',lin)                          ( Start extracting      ) Stop extractingprint(y)                                                  Result will always be a extracted portion  Spam Confidenceimport rehand = open('mbox-short.txt')numlist = list()for line in hand:    line = line.rstrip()    stuff = re.findall('^X-DSPAM-Confidence: ([0-9.]+)',line)    if len(stuff) !=1 : continue    num = float(stuff[0])    numlist.append(num)print('Maximum:', max(numlist))  Escape Character If you want a special regular expression character to just behave normally(most of the time) you prefix it with '\'import rex = 'We just received $10.00 for cookies.'y = re.findall('\$[0-9.]+',x)print(y)<jupyter_code>wazzzzzup = 'waz{3,5}up'
wazzzup = 'waz{3,5}up'
but does not match

wazup as there is only one z
aa+b*c+

a's atleast two or more
b atleast 0 or more
c atleast one or more

aaaabcc
aabbbbc
aacc
but does not match
a
\d+ files? found\?

\d+ to match number of more than one digit

files? to match file word and 's' zero or one time

found\? to match found word and \ treats ? as character and not as regex shortcut

1 file found?
2 files found?
24 files found?

but does not match

No files found.[]<jupyter_output><empty_output><jupyter_text>[1-9]\.\s+abc[1-9]      matches any digit from 1 to 9\.         treats . as a character instead of regex shortcut\s+        whitespace character one or more timesabc        letters abc1.  abc2.   abc3.      abcbut does not match4.abc '^(file.+)\.pdf$'file_record_transcript.pdffile_0.7241999.pdf but does not matchtestfile_fake.pdf.tmp ([A-Za-z]+ ([0-9]+))[A-Za-z]+  Starting with capital Letters between A-Z followed by a-z one or manyfollowed by space[0-9]+     One or more digits\dJan 1987May 1969Aug 2011 I love (cats|dogs)I love catsI love dogsdoes not matchI love logsI love cogs ^-?\d+(,\d+)*(\.\d+(e\d+)?)?$^-?          starts with - or not  ^-?\d+       starts with - or not, followed by one or more digits(,\d+)*      comma, then followed by one or more digits ------------  this pattern zero or more times(\.\d+(e\d+)?)?      followed by a "."    and then by one or more digits, then followed by letter "e" and then followed by 1 or more digits --------- this pattern present or notabove two lines present or not.3.14529-255.341281.9e10123,340.00but does not match720p<jupyter_code>import re
p = re.compile(r'\sclass\s')
print(p.search('no class at all'))              # Takes class along with spaces
p = re.compile(r'\bclass\b')
print(p.search('no class at all'))              # Takes class without spaces<jupyter_output><re.Match object; span=(2, 9), match=' class '>
<re.Match object; span=(3, 8), match='class'>
{'fraction_non_alphanumeric': 0.1399467376830892, 'fraction_numerical': 0.032223701731025296, 'mean_word_length': 3.5301568154402894, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '460245', 'license': 'Apache-2.0', 'n_tokens_mistral': 2695, 'n_tokens_neox': 2473, 'n_words': 834, 'path': 'Regular Expressions.ipynb', 'repo_name': 'sudheer1710/Remaining-Files', 'size': '7681'}