我在尝试替换字符串中的文本。首先,我必须使用findall()来收集几个不同的事件,然后我必须用一个特定的字符串替换它们。
以下是问题所在:
这是我的代码:
import re
search_string = '''This is a string to search for a regular expression
like regular expression or regular-expression or regular:expression or
regular&expression'''
pattern = re.findall('regular.expression', search_string)
substitution = 'regular expression'
replace_results = re.sub('regular.expression', 'regular expression',
search_string)
print(replace_results)因此,我知道find将返回一个列表,这将导致TypeError: unhashable:' list‘。我可以用“regular.expression”代替sub,但这并不能真正解决问题。做这件事的正确方法是什么?
发布于 2019-04-13 22:06:40
这个问题根本没有要求您使用re.findall。您应该使用re.sub搜索模式,然后用substitution替换匹配。
import re
search_string = 'This is a string to search for a regular expression like regular expression or regular-expression or regular:expression or regular&expression'
pattern = 'regular[ -:^]expression'
substitution = 'substitution'
replace_result = re.sub(pattern, substitution, search_string)
print(replace_result)这一产出如下:
This is a string to search for a substitution like substitution or substitution or substitution or substitution发布于 2020-03-29 05:13:57
import re
#The string to search for the regular expression occurrence (This is provided to the student)
search_string='''This is a string to search for a regular expression like regular expression or
regular-expression or regular:expression or regular&expression'''
#1. Write a regular expression that will find all occurrances of:
# a. regular expression
# b. regular-expression
# c. regular:expression
# d. regular&expression
# in search_string
text=re.compile(r'regular.\w+')
##2. Assign the regular expression to a variable named pattern
pattern=text
#The string to use for substitution (This is provided to the student)
substitution="regular expression"
#3. Using the sub() method from the re package substitute all occurrences of the 'pattern' with 'substitution'
result = re.sub(pattern, substitution, search_string)
#4. Assign the outcome of the sub() method to a variable called replace_result
replace_result=result
#5. Output to the console replace_results
print(replace_result)
import re
#The string to search for the regular expression occurrence (This is provided to the student)
search_string='''This is a string to search for a regular expression like regular expression or
regular-expression or regular:expression or regular&expression'''
#1. Write a regular expression that will find all occurrances of:
# a. regular expression
# b. regular-expression
# c. regular:expression
# d. regular&expression
# in search_string
text=re.compile(r'regular.\w+')
##2. Assign the regular expression to a variable named pattern
pattern=text
#The string to use for substitution (This is provided to the student)
substitution="regular expression"
#3. Using the sub() method from the re package substitute all occurrences of the 'pattern' with 'substitution'
result = re.sub(pattern, substitution, search_string)
#4. Assign the outcome of the sub() method to a variable called replace_result
replace_result=result
#5. Output to the console replace_results
print(replace_result)
https://stackoverflow.com/questions/55670161
复制相似问题