<jupyter_start><jupyter_text>Regex<jupyter_code># !pip install re
pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
email = '<EMAIL>'
import re  # Python Library used for regex operations
if re.fullmatch(pattern, email):
    print('It is a match')
else:
    print('Invalid email id. Try again')
def validate_email_address(test_your_email):
    regex_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
    if re.fullmatch(regex_pattern, test_your_email):
        print('It is a match')
    else:
        print('Invalid email id. Try again')
validate_email_address('<EMAIL>')
validate_email_address('<EMAIL>')  # Doesn't contain @
validate_email_address('<EMAIL>')
validate_email_address('<EMAIL>')
validate_email_address('<EMAIL>') # After @, before dot (.) you can only have A-Z, a-z, 0-9, . or _<jupyter_output><empty_output><jupyter_text>**Regex Tutorial**1. https://www.datacamp.com/community/tutorials/python-regular-expression-tutorial2. https://www.w3schools.com/python/python_regex.asp**Regex Checker**You can check your regex expression is working or not on texts or strings on these websites.1. https://pythex.org/2. https://regex101.com/3. http://www.pyregex.com/  re.search()<jupyter_code>texts = ['1,508 new cases confirmed in Ireland',
        '1,712 new cases confirmed in Ireland',
        '678 new cases confirmed in Ireland',
        '20 new cases confirmed in Ireland',
        'There are 200 new cases confirmed in Ireland'
       ]
texts
texts[0]
pattern = r'.*new'
re.search(pattern, texts[0])
re_obj = re.search(pattern, texts[0])
re_obj.start(), re_obj.end(), re_obj.span(), re_obj.group()
list_numbers = [re.search(pattern, text).group() for text in texts]
list_numbers<jupyter_output><empty_output><jupyter_text>In the last list element, we get 'There are' because we have .* before new.  re.findall()<jupyter_code>pattern = r'Ireland'
[re.findall(pattern, text) for text in texts]
pattern = r'\d'
[re.findall(pattern, text) for text in texts]
pattern = r'[0-9]'
[re.findall(pattern, text) for text in texts]<jupyter_output><empty_output><jupyter_text>**Note the difference -**<jupyter_code>pattern = r'\d+'  # + - one or more repetitions
text = 'There were 60 students in 5 classes'
re.findall(pattern, text)
pattern = r'\d'
text = 'There were 60 students in 5 classes'
re.findall(pattern, text)<jupyter_output><empty_output><jupyter_text>**But this also doesn't work for all instances**<jupyter_code>pattern = r'\d+'
[re.findall(pattern, text) for text in texts]<jupyter_output><empty_output><jupyter_text>re.split()<jupyter_code>space_sep_tokens = [re.split("\s", text) for text in texts]
space_sep_tokens<jupyter_output><empty_output><jupyter_text>re.sub()Hint: sub for substitute<jupyter_code>texts
space_sep_tokens = [re.sub("\d+,?\d+", "X", text) for text in texts]
space_sep_tokens
space_sep_tokens = [re.sub("\d+", "X", text) for text in texts]
space_sep_tokens
space_sep_tokens = [re.sub("\d+,\d+", "More than 1000", text) for text in texts]
space_sep_tokens<jupyter_output><empty_output><jupyter_text>Other Examples<jupyter_code>texts = '''1,508 new cases confirmed in Ireland\n712 new cases confirmed in Ireland\n678 new cases confirmed in Ireland\n20 new cases confirmed in Ireland\nThere are 200 new cases confirmed in Ireland'''
re.findall(r'.*\n', texts)
re.split('\n', texts)
text = 'Please contact us at: <EMAIL>'
match = re.search(r'([\w\.-]+)@([\w\.-]+)', text)

print("Email address:", match.group(0))  # The whole matched text
print("Username:", match.group(1))  # The username (group 1)
print("Host:", match.group(2))  # The host (group 2)
match.string
for match in re.finditer(r'([\w\.-]+)@([\w\.-]+)', text):
    print(match.span())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16466185511895215, 'fraction_numerical': 0.019246190858059342, 'mean_word_length': 5.21594684385382, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 4, 'lorem ipsum': 0, 'www.': 3, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '169592', 'license': 'MIT', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1336, 'n_words': 330, 'path': 'Week 1/1_Regex.ipynb', 'repo_name': 'BanuTK/NLP_Workshop_WAIA_Sept_2021', 'size': '3833'}