首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图在分隔符之间匹配字符

试图在分隔符之间匹配字符
EN

Stack Overflow用户
提问于 2021-04-24 09:30:54
回答 2查看 128关注 0票数 1

分隔符是-(在破折号前后有空格),文本是:Information and Telecommunication - Salaries and Wages - Non-Management。我试图匹配的文本是Information and TelecommunicationSalaries and WagesNon-Management

我得到的最接近的正则表达式是(\s-\s)?[\w\s]+(\s-\s)?,但不幸的是,匹配产生了NonManagement作为单独的匹配。

另一个要尝试的案例:Information and Telecommunication - Salaries and Wages - Non-Management - 1--2

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-24 10:17:48

您可以将空格与可选连字符-匹配,也可以将连字符与可选空格-匹配,但不匹配-

代码语言:javascript
复制
\w+(?:(?: -*|-+ ?)\w+)*

模式匹配

  • \w+匹配1+字字符
  • (?:非捕获群
    • (?: -*|-+ ?)匹配和0+ -或匹配1+倍-和可选
    • \w+匹配1+字字符

  • )*关闭非捕获组并可选择重复

Regex演示

票数 1
EN

Stack Overflow用户

发布于 2021-04-24 09:41:34

实际上,您需要的是匹配单词字符的正则表达式,或者在-或行尾之前的空格或连字符。

下面的正则表达式应该执行

代码语言:javascript
复制
(\w(?:[\w\s]|\w-\w)*)(?=(?:\s-\s)|$)

解释

代码语言:javascript
复制
(             # 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 end

演示

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67241250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档