如果有数字,正则表达式根本不匹配

2024-09-30 18:23:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试匹配此模式:

] 1 some words 2 some other words 3 some other words

但如果是以下情况,则不匹配:

] 1 some words 2 some other words 3 T

] 1 some words 2 some other words 3 some other words 4 words 5 words

例如,我想匹配以下内容:

e   fare   …   incontro]   1 p       2 perchè   dopo   quell       3 e 

这是我的尝试,但无法正常工作:

\]\s*1\s*([\w\s]+)\s*2\s*([\w\s]+)\s*3\s*([\w\s][^\W\d_T]+)

规则: -如果有“3 T”,则根本不匹配 -如果在“3”之后有4或5,则根本不匹配


Tags: 规则模式情况somewordsotherperchfare
1条回答
网友
1楼 · 发布于 2024-09-30 18:23:57

从你的正则表达式,我想我明白你在尝试什么。我猜你很接近:

(?!.*\s4\s)\]\s*1\s*([\w\s]+)\s*2\s*([\w\s]+)\s*3\s*([^\d_T]+)\b(?:\s)

在您的模式中,更改:

([\w\s][^\W\d_T]+)

([^\d_T]+)\b(?:\s)          - match any character except digit or _ or T until a word boundary followed by a non-capturing space

并添加到开头

(?!.*\s4\s)                     - do not match if line contains space+4+space

Regex Demo

相关问题 更多 >