Implement only one duplicate regular expression character

Asked 2 years ago, Updated 2 years ago, 73 views

import re

Before_str = "The name is Chul-soo Kim, the phone number is 010-0000:0000, and the place to make a reservation is Wolha Village. The number of people is nine. The date is 2019-11-11"

after_str = re.sub("[-|:|||,|/*|Phone number |Number |Name |Name ||Name ||Place ||Place ||People ||Number |Name ||Name|name|date|year|day|date|]", ", before_str) 

print(after_str)

Result value: Kim Chul 010,0000000 Wolha Village 9 20191111

(The result value comes out like this, but I know that the number of people was erased from the result value because after_str erased the number of people, but the number of Kim Chul-soo was also erased, so Kim Chul-soo came out. Is there any other coding method to make Kim Cheol-soo come out as it is? No matter how much I google it, it doesn't come out, so I'm asking you a question, masters!)

python substring regex

2022-09-21 16:54

1 Answers

A regular expression is a tool that defines a condition and returns or replaces a string that fits that condition. It's not about taking out things that don't meet the requirements.

Would you like to check this out?

import re

regex = r"name is ([^?go]+).*The phone number is (\d{2,3}\S\d{3,4}\S\d{4}).*Reservation is ([Ga-He]+)\. The number of people is (\d+)\. Date is (\d{4}-\d{2}-\d{2})"
subst = "\5\3\1Other\4 people\\2"
result = re.sub(regex, subst, before_str, 0, re.MULTILINE)


2022-09-21 16:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.