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

Member

by larissa , in category: Third Party Scripts , 2 days 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 , 2 days 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.