匹配没有至少两个分号的开括号和右括号

时间:2022-06-20 21:45:01

I have text like this:

我有这样的文字:

[Unix; common] To expand special characters in a wildcarded name, or the act of so doing (the action is also called [[b;#fff;]globbing]). The Unix conventions for filename wildcarding have become sufficiently pervasive that many hackers use some of them in written English, especially in email or news on technical topics. Those commonly encountered include the following:Some examples: [[i;;]He said his name was [KC]arl] (expresses ambiguity). [[i;;]I don't read talk.politics.*] (any of the talk.politics subgroups on [[bu;#fff;;jargon]Usenet]). Other examples are given under the entry for [[bu;#fff;;jargon]X]. Note that glob patterns are similar, but not identical, to those used in [[bu;#fff;;jargon]regexp]s.Historical note: The jargon usage derives from glob, the name of a subprogram that expanded wildcards in archaic pre-Bourne versions of the Unix shell.

[UNIX; common>扩展通配名称中的特殊字符,或者这样做的行为(该动作也称为[[b; #fff;] globbing])。文件名通配符的Unix约定已经变得非常普遍,以至于许多黑客以书面英语使用其中一些,特别是在电子邮件或技术主题的新闻中。那些经常遇到的包括:一些例子:[[i ;;]他说他的名字是[KC] arl](表达歧义)。 [[i ;;]我没有阅读talk.politics。*]([[bu; #fff ;; jargon] Usenet]上的任何talk.politics子组。其他例子在[[bu; #fff ;; jargon] X]的条目下给出。请注意,glob模式与[[bu; #fff ;; jargon] regexp] s中使用的类似,但不完全相同。历史记录:术语用法来自glob,这是一个子程序的名称,用于在古老的pre中扩展通配符-Bourne版本的Unix shell。

and I want to replace [KC] and [Unix; common] by \[KC\] and \[Unix; common\]

我想取代[KC]和[Unix;通过] [KC \]和\ [Unix;共同\]

I've try this regex:

我试试这个正则表达式:

/\[(?![^;]*;[^;]*;[^\]]*\])[^\]]+\]/g

but it don't work.

但它不起作用。

1 个解决方案

#1


2  

Just a small change. You also need to include \] along with ; inside the negated char class because [^;] should also match ], so it should exceed the closing bracket ].

只是一个小小的变化。你还需要包含\];在否定的char类中,因为[^;]也应该匹配],所以它应该超过结束括号]。

\[((?![^;\]]*;[^;\]]*;[^\]]*\])[^\]]+)\]

DEMO

#1


2  

Just a small change. You also need to include \] along with ; inside the negated char class because [^;] should also match ], so it should exceed the closing bracket ].

只是一个小小的变化。你还需要包含\];在否定的char类中,因为[^;]也应该匹配],所以它应该超过结束括号]。

\[((?![^;\]]*;[^;\]]*;[^\]]*\])[^\]]+)\]

DEMO