How to find a sentence and a 13 digit code in a paragraph in regex?

Member

by larissa , in category: Third Party Scripts , 4 months ago

How to find a sentence and a 13 digit code in a paragraph in regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , 4 months ago

@larissa 

To find a sentence and a 13 digit code in a paragraph using regex, you can use the following pattern:


Sentence pattern: "[A-Z][a-z]+[0-9] [A-Z][a-z]+[0-9]+[.!?]" Code pattern: "[0-9]{13}"


You can then combine these patterns to create a regex pattern that will match both the sentence and the 13 digit code in the paragraph. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import re

paragraph = "This is a sample sentence with a 13 digit code: 1234567890123. Another sentence with a 13 digit code: 9876543210987."

pattern = "[A-Z][a-z]+[0-9] [A-Z][a-z]+[0-9]+[.!?]"
code_pattern = "[0-9]{13}"

matches = re.findall(pattern + "|" + code_pattern, paragraph)

for match in matches:
    print(match)


This code will find and print both the sentences and the 13 digit codes in the given paragraph.

Related Threads:

How to match a sentence between 2 words using regex?
How to extract data in paragraph using regex?
How to set max limit for each word in the sentence using regex in java?
How to extract specific digit from pandas column using regex?
How to find all the xpaths of the text in html code?
How to find repeated word in a string with regex?