Python Regex: How Find a Substring in a String

Channel:
Subscribers:
53,100
Published on ● Video Link: https://www.youtube.com/watch?v=iESyyogOkY0



Duration: 4:12
21,835 views
254


↓ Code Available Below! ↓

This video shows how to find and extract substrings within python strings using the regular expressions package. Locating and extracting substrings from larger strings is a common task in text data processing; Python makes it easy to do these sorts of text data processing tasks.

If you find this video useful, like, share and subscribe to support the channel!
► Subscribe: https://www.youtube.com/c/DataDaft?sub_confirmation=1

**Note** if you want to match a specific substring (such as a specific word) and not a regular expression pattern, just pass that word or substring in as the "pattern" argument.

Code used in this Python Code Clip:

import re

lines = '''
Nappa - What does the scouter say about his power level?
Vegeta - It's over 9000!
Nappa - What 9000? That can't be right. Can it?
'''

# Use re.search() to match the first instance of a regex

first_match = re.search(pattern = "[0-9]+",
string = lines)

first_match

# Extract the matched string from the match:

first_match.group()

# Extract the index positions of the start and end:

print(first_match.start())
print(first_match.end())

# Use re.finditer() to find all matches:

all_matches = re.finditer(pattern = "[0-9]+",
string = lines)

for match in all_matches:
print(match.group(), match.start(), match.end())

# Use re.findall() to get all matches as a list of strings

all_match_list = re.findall(pattern = "[0-9]+",
string = lines)

all_match_list


* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .


⭐ Kite is a free AI-powered coding assistant that integrates with popular editors and IDEs to give you smart code completions and docs while you’re typing. It is a cool application of machine learning that can also help you code faster! Check it out here: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=datadaft&utm_content=description-only







Tags:
python find substring
python regex
python find all substrings
python match
python search
python search()
re.search()
re.finditer()
re.findall()
python match object
python string matching
python text processing
python find substring in text
python locate substring
python detect substring
find substring
regex find substring
regex get all matches
python regular expressions
python re
python text data
text data
regular expressions
regex
text processing