How to extract data in paragraph using regex?

by tressie.damore , in category: Third Party Scripts , 4 months ago

How to extract data in paragraph using regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raphael_tillman , 4 months ago

@tressie.damore 

To extract data from a paragraph using regex, you can use the re module in Python. Here is an example of how you can extract data in a paragraph using regex:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import re

# Sample paragraph
paragraph = "This is a sample paragraph with some data such as phone numbers like 123-456-7890 or email addresses like example@gmail.com."

# Define a regex pattern to match phone numbers
phone_pattern = r'd{3}-d{3}-d{4}'

# Define a regex pattern to match email addresses
email_pattern = r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z|a-z]{2,}'

# Find all phone numbers in the paragraph
phone_numbers = re.findall(phone_pattern, paragraph)

# Find all email addresses in the paragraph
email_addresses = re.findall(email_pattern, paragraph)

print("Phone Numbers:", phone_numbers)
print("Email Addresses:", email_addresses)


In this example, we first define regex patterns to match phone numbers and email addresses. We then use the re.findall() function to extract all occurrences of these patterns in the paragraph. Finally, we print out the extracted phone numbers and email addresses. You can customize the regex patterns to extract different types of data from the paragraph.

Related Threads:

How to extract part of string using regex?
How to extract specific from the url using regex?
How to extract part of string in bash using regex
How to extract parameter definitions using regex?
How to extract json from html source code using regex?
How to extract specific digit from pandas column using regex?