匹配字符串中第一次出现的分号,只有前缀为' - '

Ric*_*ard 5 java regex

我正在尝试编写一个Java的正则表达式,如果有一个分号没有两个(或更多)前导' - '字符,则匹配.

我只能做相反的工作:一个至少有两个前导' - '字符的分号.

([\-]{2,}.*?;.*)
Run Code Online (Sandbox Code Playgroud)

但我需要类似的东西

([^([\-]{2,})])*?;.*
Run Code Online (Sandbox Code Playgroud)

我不知道怎么说不能表达"至少两个字符".

以下是我需要使用表达式评估的一些示例:

; -- a           : should match
-- a ;           : should not match
-- ;             : should not match
--;              : should not match
-;-              : should match
---;             : should not match
-- semicolon ;   : should not match
bla ; bla        : should match
bla              : should not match (; is mandatory)
-;--;            : should match (the first occuring semicolon must not have two or more consecutive leading '-')
Run Code Online (Sandbox Code Playgroud)

Ada*_*ost 0

你需要一个消极的前瞻!

此正则表达式将匹配任何不包含原始匹配模式的字符串:

(?!-{2,}.*?;.*).*?;.*
Run Code Online (Sandbox Code Playgroud)

此正则表达式匹配包含分号的字符串,但不匹配出现在 2 个或多个破折号之后的分号。

例子: 正则表达式工作