分隔符是-(在破折号前后有空格),文本是:Information and Telecommunication - Salaries and Wages - Non-Management。我试图匹配的文本是Information and Telecommunication、Salaries and Wages和Non-Management
我得到的最接近的正则表达式是(\s-\s)?[\w\s]+(\s-\s)?,但不幸的是,匹配产生了Non和Management作为单独的匹配。
另一个要尝试的案例:Information and Telecommunication - Salaries and Wages - Non-Management - 1--2
发布于 2021-04-24 10:17:48
您可以将空格与可选连字符-匹配,也可以将连字符与可选空格-匹配,但不匹配-。
\w+(?:(?: -*|-+ ?)\w+)*模式匹配
\w+匹配1+字字符(?:非捕获群(?: -*|-+ ?)匹配和0+ -或匹配1+倍-和可选\w+匹配1+字字符)*关闭非捕获组并可选择重复发布于 2021-04-24 09:41:34
实际上,您需要的是匹配单词字符的正则表达式,或者在-或行尾之前的空格或连字符。
下面的正则表达式应该执行
(\w(?:[\w\s]|\w-\w)*)(?=(?:\s-\s)|$)解释
( # matching group start
\w # match any word character once
(?: # start of a non-matching group
[\w\s] # match any word character or space character
| # OR
\w-\w # match any hyphen that's in between two word characters
)* # find this non-matching group zero or multiple times
) # matching group end
(?= # lookahead start
(?:\s-\s) # non-matching group for space hyphen space
| # OR
$ # end of string
) # lookahead endhttps://stackoverflow.com/questions/67241250
复制相似问题