regex - 正则表达式检测具有 .png 且介于 [ 和 ] 之间的字符串
问题描述
我正在寻找一种正则表达式模式,它可以检测 [ 和 ] 之间的任何字符串并在其中包含 .png 。例如 [任何与 .png 匹配的内容] 到目前为止,我有这个:“\[[^>]+]”,它检测 [ 和 ] 之间的任何内容,但我只想包含具有 .png 的字符串
我对正则表达式没有经验。
如果我通过“abc [abc.png] abc [abc] abc”我想得到 [abc.png]
如果我通过 "abc [abc.pngabc] abc [abc] abc" 我想得到 [abc.pngabc]
解决方案
如果您只想要括号之间以 .png 结尾的字符串,请尝试以下操作:(假设您的字符串始终以“.png]”结尾)
//this will find a string of any length that ends with .png and is surrounded by []
(?<=\[)\S*\.png(?=\])
\\(?<=...) is called a positive lookbehind and checks if something is adjacent to the left of your match
\\(?=...)Positive lookbehind checks for something to the right
如果您还想包含 [] ,请使用:(这使用非方括号字符,因为它假定它们是您的字符串中唯一不能包含或分隔字符串的内容。完全健壮。
\[[^\]\[]*\.png[^\]\[]*\]
[abc.png] abc [abc] [hey.png]
\\will return
[abc.png] [hey.png]
推荐阅读
- encryption - 在 ActiveMQ 中加密 Web 控制台密码
- php - 创建插件域的子域
- initialization - 如何修复“不能在属性初始化器中使用实例成员'textField_1';属性初始化器在'self'可用之前运行”?
- c++ - C++:对于一个继承了多个类的类,如何从一个类继承某些属性而从另一个类继承其他属性?
- angular - 如何以角度显示 Highchart 金字塔内的系列数据标签
- python - 如何将 DF 的新结果附加到旧输出文件(带日期时间)
- amazon-web-services - S3 存储桶策略授予/限制对特定联合用户的访问权限
- spring-boot - 如何在 SpringBoot 应用程序中使用 hikari 配置自动重连?
- python - 面临使用 SetSourceData 方法通过 python 在 powerpoint 中绘制图表的问题
- javascript - Django 搜索过滤器与 Javascript 查询搜索过滤器?