首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在findall()函数返回的列表上运行sub()函数?

如何在findall()函数返回的列表上运行sub()函数?
EN

Stack Overflow用户
提问于 2019-04-13 21:49:50
回答 2查看 248关注 0票数 1

我在尝试替换字符串中的文本。首先,我必须使用findall()来收集几个不同的事件,然后我必须用一个特定的字符串替换它们。

以下是问题所在:

  1. 编写一个正则表达式,该表达式将在search_string中找到所有出现的正则表达式b.正则表达式c.正则:表达式d正则表达式
  2. 将正则表达式赋值给名为模式的变量。
  3. 使用re包中的sub()方法将“模式”的所有出现替换为“替换”
  4. 将sub()方法的结果分配给一个名为replace_result的变量
  5. 输出到控制台replace_results

这是我的代码:

代码语言:javascript
复制
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,但这并不能真正解决问题。做这件事的正确方法是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-13 22:06:40

这个问题根本没有要求您使用re.findall。您应该使用re.sub搜索模式,然后用substitution替换匹配。

代码语言:javascript
复制
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)

这一产出如下:

代码语言:javascript
复制
This is a string to search for a substitution like substitution or substitution or substitution or substitution
票数 2
EN

Stack Overflow用户

发布于 2020-03-29 05:13:57

代码语言:javascript
复制
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)

代码语言:javascript
复制
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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55670161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档