使用Regex将每个匹配实例替换为不同的字符串

时间:2017-04-30 14:04:31

标签: python regex python-3.x

我目前正在努力“使用python自动化无聊的东西”。我参加了第8​​章“疯狂的自由人”的练习项目。任务如下:

  

创建一个Mad Libs程序,该程序读入文本文件并允许用户添加   他们自己的文字ADJECTIVE,NOUN,ADVERB或VERB   出现在文本文件中。例如,文本文件可能如下所示:

The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.
     

程序会找到这些事件并提示用户   替换它们。

     

输入一个形容词:傻瓜

     

输入名词:枝形吊灯

     

输入动词:尖叫

     

输入名词:皮卡车

     

然后将创建以下文本文件:

The silly panda walked to the chandelier and then screamed. A nearby pickup truck was unaffected by these events.
     

结果应打印到屏幕上并保存到新的文本文件中。

我目前的计划如下:

#! python3
# Requests user for an ADJECTIVE, NOUN, ADVERB, and a NOUN
# Replaces the words ADJECTIVE/NOUN/ADVERB/NOUN with the input in a txt file
# Saves the new Mad Lib as a new txt file

import re

reADJECTIVE = re.compile(r'''(ADJECTIVE)''')
reNOUN = re.compile(r'''(NOUN)''')
reVERB = re.compile(r'''(VERB)''')

for i in range(1):
    # Gets user input for ADVECTIVE/NOUN/VERB/NOUN
    ADJECTIVE = input('Enter an adjective: ')
    NOUN = input('Enter a noun: ')
    VERB = input('Enter a verb: ')
    NOUN2 = input('Enter a noun: ')

    madLibFile = open('madlib%s.txt' % (i + 1))
    madLibFileContent = madLibFile.read()
    madLibFile.close()
    madLibFileContent = madLibFileContent.split('. ')
    print(madLibFileContent)

    newMadLib = re.sub(reADJECTIVE, ADJECTIVE, madLibFileContent[0])
    newMadLib = re.sub(reNOUN, NOUN, newMadLib)
    newMadLib = re.sub(reVERB, VERB, newMadLib)
    newMadLib = newMadLib + '. ' + re.sub(reNOUN, NOUN2, madLibFileContent[1])

    print(newMadLib)

对于给定的示例,此程序可以正常工作,但是由于我分隔文件的方式,它通过fullstop / period读取它只在输入文件的格式为:

时才有效
  

ADJECTIVE NOUN ADVERB。名词。

并不适用于任何其他格式,例如:

  

ADJECTIVE NOUN。 ADVERB NOUN。

我最初的想法是使用正则表达式模式:

(ADJECTIVE).*(NOUN).*(VERB).*(NOUN)

如果我们假设任何给定的Mad Lib遵循相同的Adjective-Noun-Verb-Noun模式,则此方法有效。

如果我要使用:

re.sub(r'(NOUN)', replacement, someString)

它将替换字符串中的NOUN实例。是否可以用不同的东西替换每个捕获组?

感谢您的时间,我希望这个问题足够明确:)

2 个答案:

答案 0 :(得分:2)

诀窍是使用函数代替re.sub中的替换字符串。这是一种粗暴的做法。

import re

sentence = 'The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.'

def consider(matchobj):
    content = matchobj.group()
    if content in ['NOUN', 'ADJECTIVE', 'ADVERB', 'VERB']:
        return input('Please enter ' + content)
    else:
        return content

print (re.sub('[A-Z]+', consider, sentence))

我没有费心去用你的话,就像那时我心烦意乱的事情。这是它在使用中的样子。

Please enter ADJECTIVEbig
Please enter NOUNbear
Please enter VERBgoes
Please enter NOUNhome
The big panda walked to the bear and then goes. A nearby home was unaffected by these events.

编辑:已添加以回复评论。

import re

partsOfSpeech = ['NOUN', 'ADJECTIVE', 'ADVERB', 'VERB']
replacements = {_:'' for _ in partsOfSpeech}

for r in replacements:
    replacements[r] = input('Please enter ' + r.lower() + ': ')

madLibs = [
    'The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.',
    'A NOUN ADVERB decided to VERB the NOUN'
    ]

def consider(matchobj):
    content = matchobj.group()
    if content in partsOfSpeech:
        return replacements[content]
    else:
        return content

for madLib in madLibs:
    print (re.sub('[A-Z]+', consider, madLib))

结果:

Please enter adjective: vast
Please enter adverb: smoothly
Please enter verb: went
Please enter noun: bear
The vast panda walked to the bear and then went. A nearby bear was unaffected by these events.
A bear smoothly decided to went the bear

答案 1 :(得分:0)

这是我的代码:

#! python3
# Mad Libs program to replace ADJECTIVE, NOUN, ADVERB, or VERB
import re,os
dir=os.getcwd()
readFile=open(dir)
contents=readFile.read()
readFile.close()
content=contents.split()

好的,以上几行打开了文件,并且文件的全部内容都存储在contents中。在content中,各个单词是独立存储的。

i=0
for contents in content:
    if('ADJECTIVE' in contents):#Checks if the word is ADJECTIVE
        regex1=re.compile(r'ADJECTIVE')
        content[i]=regex1.sub(input('What is the ADJECTIVE?\nEnter:'),contents)
    elif ('NOUN' in contents):#Checks if the word is NOUN
        regex1=re.compile(r'NOUN')
        content[i]=regex1.sub(input('What is the NOUN?\nEnter:'),contents)
    elif ('VERB' in contents):#Checks if the word is VERB
        regex1=re.compile(r'VERB')
        content[i]=regex1.sub(input('What is the VERB?\nEnter:'),contents)
    elif ('ADVERB' in contents):#Checks if the word is adverb
        regex1=re.compile(r'ADVERB')
        content[i]=regex1.sub(input('What is the ADVERB?\nEnter:'),contents)
    i=i+1

regex1负责代替VERB,ADJECTIVE,NOUN和ADVERB的各个输入。 content[i]i,以确保位置和正确的单词被替换。

contents=' '.join(content)
print(contents)
writeFile=open(dir,'w')
writeFile.write(contents)
writeFile.close()

请随时纠正我,或者以较短的方式解决此问题。希望它能解决您的问题。