简体   繁体   English

不带高级正则表达式

[英]high-level regular expression with not

Hi regular expression experts, 嗨,正则表达式专家,

I have the following text 我有以下文字

<[~UNKNOWN:a-z\.]> <[~UNKNOWN:A-Z\-0-9]> <[~UNKNOWN:A-Z\]a-z]

And the following reg expr 和以下reg expr

\[\~[^\[\~\]]*\]

It works fine for the 1st and 2nd group in the text but not for the 3rd one. 它适用于文本中的第一组和第二组,但不适用于第三组。

The 1st group is 第一组是

[~UNKNOWN:a-z\.]

The 2nd is 第二个是

[~UNKNOWN:A-Z\-0-9]

and the 3rd one is 第三个是

[~UNKNOWN:A-Z\]a-z]

However the reg exp finds the following text 但是,reg exp发现以下文本

[~UNKNOWN:A-Z\]

I understand why and I know that I have to add the following rule to the reg exp: 我理解原因,并且知道必须在reg exp中添加以下规则:
starting with '[' and '~' characters and ending with ']' UNLESS there is a '\\' in front of ']'. 以'['和'〜'字符开头,以']'结尾,除非']'前面有'\\'。 So I should add a NOT expression but not sure how. 所以我应该添加一个NOT表达式,但不确定如何。

Could anybody please help? 有人可以帮忙吗?

Thanks, 谢谢,
V. V.

Why not simply: 为什么不简单:

<([^>]+)>?

在此处输入图片说明


Regex Demo 正则表达式演示

This should work (first line pattern, second line your pattern (ignore whitespace), third line my changes): 这应该工作(第一行模式,第二行您的模式(忽略空格),第三行我的更改):

\[\~(?:[^\[\~\]]|(?<=\\)\])*(?<!\\)\]
\[\~   [^\[\~\]]           *       \]
    (?:         |(?<=\\)\]) (?<!\\)

Your regex: 您的正则表达式:

\[\~             # Literal characters [~
[^               # Character group, NONE of the following:
    \[\~\]       # [ or ~ or ]
]*               # 0 or more of this character group
\]               # Followed by ]

Your pattern in words: [~ , everything in between, up to the next ] , as long as there is no [ or ~ or ] in there. 您的语言模式: [~ ,介于两者之间,直到下一个] ,只要其中没有[~]

My pattern , only relevant changes explained: 我的模式,只有相关的更改说明了:

\[\~             
(?:              # Non capturing group
    [^\[\~\]]    
    |            # OR
    (?<=\\)\]    # ], preceded by \
)*               
(?<!\\)\]         # ], not preceded by \

In words: Same as yours, plus ] may be contained if it is preceded by \\ , and the closing ] may not be preceded by \\ 换句话说:与您的字母相同,如果在\\前面加上]可能包含在内,而在]的结尾不包含\\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM