python - 使用 re.search 在 python 中找不到特定的子字符串
问题描述
我想在这里静音“name_list”并扫描列表中的每个字符串以查找“nametosearch”中的术语列表。对于每个带有“$”符号的术语,代码应从“name_list”中删除该特定字符串。
相反,我的代码适用于“$placeholder”,但根本不适用于“$hidden”。
当一切都完成后,我只想要
<input class="form-control" maxlength="20" name="auth_user_name" size="28" type="text" value=""/>
留下来,相反我得到:
<input name="ib_s" size="64" type="hidden" value="e44b06a472945566fca51723110ab34a">new user?', '<input class="form-control" maxlength="20" name="auth_user_name" size="28" type="text" value=""/>
我哪里错了?(代码贴在下面)
import re
name_list=['<input class="form-control" name="keywords" placeholder="enter search term, ad #, or username" type="text" value="">', '<input name="ib_s" size="64" type="hidden" value="e44b06a472945566fca51723110ab34a">new user?', '<input class="form-control" maxlength="20" name="auth_user_name" size="28" type="text" value=""/>']
nametosearch=('user', '$placeholder', '$hidden')
for x3 in name_list:
for z2 in nametosearch:
if z2[0]=='$' and re.search(z2[1:],x3):
name_list.remove(x3)
很高兴提供更多详细信息,我真的不知道您可能还需要什么,但请询问。
编辑:'nametosearch' 是一个元组,因为内容是函数的参数。
谢谢
解决方案
import re
name_list=['<input class="form-control" name="keywords" placeholder="enter search term, ad #, or username" type="text" value="">', '<input name="ib_s" size="64" type="hidden" value="e44b06a472945566fca51723110ab34a">new user?', '<input class="form-control" maxlength="20" name="auth_user_name" size="28" type="text" value=""/>']
nametosearch=('user', '$placeholder', '$hidden')
index_to_remove=[]
for x3,i in zip(name_list,range(len(name_list))):
for z2 in nametosearch:
if z2[0]=='$' and re.search(z2[1:],x3):
index_to_remove.append(i)
newlist= [i for j, i in enumerate(name_list) if j not in index_to_remove]
print(newlist)
输出:
['<input class="form-control" maxlength="20" name="auth_user_name" size="28" type="text" value=""/>']
推荐阅读
- postgresql - 无法在 pgAdmin 4 上查询数据,但可以使用 psql 和 Python
- amazon-web-services - 手动触发 AWS CloudWatch 事件
- css - JSX 中有什么反应
我有一个简单的反应组件,它在 JSX 属性
css
中,我理解它是用于样式的这个道具,但不清楚它是否是“本机”css 反应属性,或者它是否来自styled-components
其他库。如果不是反应库,您是否知道它来自哪个库?
<div css={{color: 'red', backgroundColor:'bl
- java - 从弹簧休息控制器返回对象中的文件
- python - 数据帧上的布尔值
- java - 支持 Ant 样式或 glob 模式的 JGit 路径过滤器
- c - 使用 freeRTOS 队列 API 或使用自定义数据结构在 freeRTOS 应用程序中创建队列更好吗?
- reactjs - 如何在 React JS 上重置组件 Dropzone?
- typescript - 我可以直接访问 DI 注入器以使用 NestJS 实例化类吗
- c# - 将 MediatR 从 MVC 项目移动到 Razor Pages。无法使基本语法正常工作