Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何找到连字符前后的空格?即“这是我的连字符示例测试”
我需要在:“my hyphenated-word”和“hyphenated-word example”之间有空格
您可以使用lookahead 和lookbehind。
之前的空间:
\s(?=([^\s]+-[^\s]+))
后面的空格:
(?<=([^\s]+-[^\s]+))\s
如果您只想获取连字符本身,请使用此正则表达式检索整个匹配项(例如捕获组 0):
\w+(?:-\w+)+