使用正则表达式“清理”名称列表

时间:2013-08-25 03:12:05

标签: python regex python-3.x

我正在使用Regex清理名称列表,以便它们正常。让我们说这个清单是......

000000AAAAAARob Alsod     ## Notice multiple 0's and A's?
AAAPerson Person          ## Here, too
Jeff the awesome Guy      ## Four words...
Jenna DEeath              ## A name like this can exist.
GEOFFERY EVERDEEN         ## All caps
shy guy                   ## All lowercase
Theone Normalperson       ## Example name. This one is fine.
  Guywith Whitespace      ## Trailing or leading whitespace is a nono.

所以,正如你所看到的,人们没有正确地格式化他们的名字,所以我需要一个程序来突出显示所有不需要的东西。这包括:

  • 名称​​开始的数字。

  • 任何大写后面没有小写。即AAAAAAAJosh

  • 任何全部大写的内容。

  • 任何不以开头的大写字母。即乔什

  • 尾随和领先的空白。

我认为这就是我需要过滤的全部内容。最终产品应如下所示:

Rob Alsod              ## No more 0's and A's.
Person Person          ## No more leading A's (or other letters).
Jeff Guy               ## No lowercase words in his name.
Jenna DEeath           ## HASN'T removed the D in the middle.
                       ## Name removed as it was all uppercase.
                       ## Name removed as it was all lowercase.
Theone Normalperson    ## Nothing changed.
Guywith Whitespace     ## Removed whitespace.
编辑:抱歉。这是我目前的代码:

# Enter your code for "Name Cleaning" here.
import re
namenum = []
num = 0
for sen in open('file.txt'):
  namenum += [sen.split(',')]
  namenum[num][0] = re.sub(r'\s[a-z]+', '', namenum[num][0])
  namenum[num][0] = re.sub(r'^([0-9]*)', '', namenum[num][0])
  namenum[num][0] = re.sub(r'^[A-Z]*?\s[A-Z]*?$', '', namenum[num][0])
  namenum[num][0] = re.sub(r'[^a-zA-Z ][A-Z]*(?=[A-Z])', '', namenum[num][0])
  namenum[num][0] = re.sub(r'\b[a-z]+\b', '', namenum[num][0])
  namenum[num][0] = re.sub(r'^\s*', '', namenum[num][0])
  namenum[num][0] = re.sub(r'\s*$', '', namenum[num][0])  
  if namenum[num][0] == '':
    namenum[num][0] = 'Invalid Name'
  num += 1
for i in range(len(namenum)):
  namenum[i][1] = int(namenum[i][1].strip())
namenum = sorted(namenum, key=lambda item: (-item[1], item[0]))
for i in range(0, len(namenum)):
  print(namenum[i][0]+','+str(namenum[i][1]))

它完成了一半的工作,但由于某种原因,它错过了一些东西。

这是输出:

AAAAAARob Alsod
AAAPerson Person
Guywith Whitespace
Invalid Name
Invalid Name
Jeff Guy
Jenna DEeath
Theone Normalperson

我还尝试输入harry hamilton之类的名称,然后返回harry,它本应删除。

1 个答案:

答案 0 :(得分:1)

此正则表达式删除所有无效示例。你的例子都不需要for循环过滤禁止的单词,但我认为你需要它。

虽然此代码从列表中删除了所有无效名称,但应该很容易对其进行修改以请求来自用户的新输入。此外,它不会让您知道名称无效的原因,但您可以只显示所有规则。

from re import match

def rules(name):
    for badWord in bannedWords:
        if name.lower().find(badWord) >= 0:
            return False        
    return match(r'^([A-Z][a-z]+(?:[A-Z]?[a-z]+)* ?){1,}$', name)

bannedWords = ('really', 'awesome')
input = ['000000AAAAAARob Alsod', 'AAAPerson Person', 'Jeff the awesome Guy', 'Jenna DEeath', 'GEOFFERY EVERDEEN', 'shy guy', 'Theone Normalperson', '  Guywith Whitespace', 'Someone Middlename MacIntyre', '', 'Jack Really Awesome']
results = filter(rules, input)
print results

产生结果:

['Theone Normalperson', 'Someone Middlename MacIntyre']

没有for循环:

from re import match

def rules(name):     
    return match(r'^([A-Z][a-z]+(?:[A-Z]?[a-z]+)* ?){1,}$', name)

input = ['000000AAAAAARob Alsod', 'AAAPerson Person', 'Jeff the awesome Guy', 'Jenna DEeath', 'GEOFFERY EVERDEEN', 'shy guy', 'Theone Normalperson', '  Guywith Whitespace', 'Someone Middlename MacIntyre', '', 'Jack Really Awesome']
results = filter(rules, input)
print results

产生结果:

['Theone Normalperson', 'Someone Middlename MacIntyre', 'Jack Really Awesome']