简体   繁体   中英

Regular expression - Python [list query]

I am trying to write a regular expression for this list:

data= ["Fred is Deputy Manager. He is working for MNC.", "Rita is another employee in AC Corp."]

And I want to delete all the words that starts with an uppercase letter but it should not check the first word of every sentence ie, it should not check for Fred, He and Rita.

The output should be

Output-["Fred is. He is working for.", "Rita is another employee in."]

I tried looking for solution but couldn't find any relevant code. Any help would be appreciated.

Thanks.

You will need to find and remove all capital words not following punctuation, then find and remove trailing spaces (this solution isn't the cleanest but it works). List comprehensions come in handy here as well.

import re

data = ["Fred is Deputy Manager. He is working for MNC.", "Rita is another employee in AC Corp."]
# find and replace all capital words that don't follow punctuation with ''
text = [re.sub(r'(?<!\.\s)(?!^)\b([A-Z]\w*(?:\s+[A-Z]\w*)*)', '', item) for item in data]
# find and remove all trailing spaces before periods
output = [re.sub(r'\s([?.!"](?:\s|$))', r'\1', item) for item in text]

>>> output
['Fred is. He is working for.', 'Rita is another employee in.']

First, let me just apologize for how unhelpful the regular expressions documentation for python 3 is. All the info to answer this question is can technically be found here , but you already need to know a bit about how re works to make sense of it. That being said, hopefully this will give you a leg up:

A simple answer

Here's some code you could try:

import re

data = ["Fred is Deputy Manager. He is working for MNC.", "Rita is another employee in AC Corp."]

matcher = re.compile("(?<![.])[ ][A-Z][A-z]*")
print([matcher.sub("",d) for d in data])
# prints: ['Fred is. He is working for.', 'Rita is another employee in.']

Basically, this compiles a regular expression which will match capital words not following a period:

  • (?<.[.]) -> don't match if preceded by a period
  • [ ][AZ][Az]* -> any capitalized word (which has a leading space, which makes sure if never matches the first word in the string)

Then, it applies that regular expression to each string in your list and replaces the matches with the empty string: ""

Some Limitations

If your strings ever have double spaces or other whitespace characters (like tabs or carriage returns) that will break this. You can fix that by instead using:

matcher = re.compile("(?<![.])\s+[A-Z][A-z]*")

where \s+ will match one or more whitespace characters

Also, if your strings ever lead off with a space, that will also break this. You can fix that by using:

print([matcher.sub("",d.strip(" ")) for d in data])

to remove the leading or trailing whitespace characters from your string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM