<jupyter_start><jupyter_text>REGEXA single expression, commonly called a regex, is a stringformed according to the regular expression language. Regular ExpressionsRegular expressions provide a flexible way to search or match (often more complex)string patterns in text.  re-module:re module functions fall into three categories:1) pattern matching,2) substitution,3) splitting. Uses of Regex Naturally these are all related; a regex describes a pattern to locate in thetext, which can then be used for many purposes. Let’s look at a simple example:suppose we wanted to split a string with a variable number of whitespace characters(tabs, spaces, and newlines). eg text = "foo bar\t baz \tqux"<jupyter_code>import re
import pandas as pd
import numpy as np
text="foo bar\t baz \tqux"
re.split('\s+',text)<jupyter_output><empty_output><jupyter_text>Compiling regex You can compile the regex yourselfwith re.compile, forming a reusable regex object:<jupyter_code>regex=re.compile('\s+')
regex.split(text)<jupyter_output><empty_output><jupyter_text>1) Regex all patterns matchinginstead, you wanted to get a list of all patterns matching the regex, you can use thefindall method:<jupyter_code>regex.findall(text)<jupyter_output><empty_output><jupyter_text>Regex ObjectCreating a regex object with re.compile is highly recommended if you intend toapply the same expression to many strings; -doing so will save CPU cycles.-match and search are closely related to findall.-While findall returns all matchesin a string, -search returns only the first match.-More rigidly, match only matches atthe beginning of the string.<jupyter_code>text = """Dave <EMAIL>
Steve <EMAIL>
Rob <EMAIL>
Ryan <EMAIL>
"""
# regex.findall(text)
pattern = r'[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}'
regex=re.compile(pattern,flags=re.IGNORECASE)
regex.findall(text)<jupyter_output><empty_output><jupyter_text>2a) Searchsearch returns a special match object for the first email address in the text. For the preceding regex, the match object can only tell us the start and end position of thepattern in the string:<jupyter_code>m = regex.search(text)
m
text[m.start():m.end()]<jupyter_output><empty_output><jupyter_text>regex.match returns None, as it only will match if the pattern occurs at the start of thestring:<jupyter_code>print(regex.match(text))<jupyter_output>None<jupyter_text>2)b SUBSTITUTION: Relatedly, sub will return a new string with occurrences of the pattern replaced by the a new string:<jupyter_code>print(regex.sub('REDACTED',text))<jupyter_output>Dave REDACTED
Steve REDACTED
Rob REDACTED
Ryan REDACTED<jupyter_text>3)SEGMENTATIONSuppose you wanted to find email addresses and simultaneously segment eachaddress into its three components:username, domain name, and domain suffix. To do this, put parentheses around the parts of the pattern to segment:<jupyter_code>pattern = r'([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})'
regex=re.compile(pattern,flags=re.IGNORECASE)<jupyter_output><empty_output><jupyter_text>A match object produced by modified regex returns a tuple of pattern components with its groups method<jupyter_code>m=regex.match('<EMAIL>')
m.groups()
regex.findall(text)
t1='United Kingdom of Great Britain and Northern Ireland19'
pattern = r'([a-zA-Z\s]+)([0-9]+)'
# pattern=r'\d', r' \(([^)]+)\)'
regex=re.compile(pattern)
m=regex.match(t1)
m.group(1)
m.group(2)
regex.findall(t1)
regex.split(t1)
# Method two on df column

df = pd.DataFrame({'iso': ['4He', '16', '197Au']})
df
# spliting a column in to alphabaets and numeric

result = df['iso'].str.split('(\d+)([A-Za-z]+)', expand=True)
result
result = result.loc[:,[1,2]]
result
##### Method 3



df['num']=df['iso'].str.extract('(\d+)').astype(int)
df
# trying to seperate alphabaetic words (but not  helping)

df['alpha']=df['iso'].str.extract('a-zA-z\s+').astype(int)
df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12127494169473957, 'fraction_numerical': 0.009847110650427572, 'mean_word_length': 6.3804971319311665, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '172710', 'license': 'MIT', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1247, 'n_words': 402, 'path': 'regex.ipynb', 'repo_name': 'wahmed555/REGEX', 'size': '3896'}