regex - 无法不匹配子字符串和匹配字符串
问题描述
我对正则表达式很陌生,我一直在努力寻找适合我需要的正确表达式,它如下所示:我需要获取字符串是否以“n”或“p”开头,为此我有 ^ (n|p)。接下来可能发生任何三种组合,为此我有(fcombination|scombination|tcombination),之后我的问题开始上升。如果“hvt”发生在之前的条件之后,我需要匹配,但如果它只是“hv”而不是“hv”,则任何子字符串都应该匹配。
有哪位高手可以帮忙吗?
问候
解决方案
如果我了解您的需求,则正则表达式^[np][fst]combination(?!hv(?!t)).*
将匹配
nfcombinationhvt //match
pscombinationhvt //match
ntcombinationhvt //match
nfcombinationdrums //match
pscombinationguitar //match
ntcombinationkicker //match
nfcombinationhvxxx //no match
pscombinationhvzz //no match
ntcombinationhva //no match
nfcombinationhv //no match
pscombinationhv //no match
ntcombinationhv //no match
推荐阅读
- sapui5 - 我可以根据屏幕宽度自动折叠 OpenUI5 侧边导航吗?
- php - 用特定元素值替换数组
- python - 如何使用python从邮票中提取文本?
- ruby-on-rails - 如何在不将文件保存在数据库中的情况下设置邮件附件?
- python - 如何通过许多站点在 BeautifulSoup 调用中存储 html 元素的变体?
- perl - Perl Tk:帮助我修复文本编辑器中的突出显示
- amazon-web-services - 有没有办法导出和导入 Amazon Connect - 联系流
- powerbi - 如果有超过 7 个句点,则计算 Distinct ID
- javascript - 如何获得2个数组的总和
- mysql - 如何将 TEXT 列转换为可索引字段?