正则表达式 - 匹配单字符之前的单词,但不匹配双字符?
冰岛:国家\n欧洲::大陆\n雷克雅未克:城市
具有上述字符串,我想匹配出现在单个冒号之前的单词,但不匹配出现在双冒号之前的单词。另外,如果单个冒号之前有空格,我希望它被忽略并匹配空格之前的单词。
所以从上面的字符串我想匹配“冰岛”(没有尾随空格)和“雷克雅未克”。
我认为创建捕获组会起作用,但事实并非如此:
/(\w+(?=:))(?!=::)/gm
我不知道如何忽略单个冒号之前的空格。
Iceland :Country\nEurope::Continent\nReykjavik:City
with the above string, i would like to match words that appear before a single colon, but not those before a double-colon. additionally, if there is whitespace before a single colon, i would like it to be ignored and match the word before the whitespace.
so from the above string i would like to match "Iceland"(without the trailing space) and "Reykjavik".
i thought that creating capture groups would work, but it doesn't:
/(\w+(?=:))(?!=::)/gm
i don't know how to ignore white space before a single colon.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您只是否定前瞻断言的语法错误......它是 (?!pattern),而不是 (?!=pattern)。
所以尝试:
[编辑 - 错过了问题的其他部分]
要忽略空格,只需匹配它但不捕获它。
...打印“那里”。
I think you just have the syntax wrong for the negative look-ahead assertion... It's (?!pattern), not (?!=pattern).
So try:
[edit - missed the other part of the question]
To ignore the whitespace, just match it but do not capture it.
...prints "there".
看来这对你来说可能是一场胜利
Look like it might be a win for you