如何制作一个带有后行断言的正则表达式,该断言在字符串开头仍然有效
我需要在字符串开头模拟 \b
的行为,其中我向集合中添加了额外的字符,这些字符被视为单词边界。现在我正在使用类似的东西:
"(?<=\\W|\\p{InCJKUnifiedIdeographs})foo"
这按照我想要的方式工作,除非我位于匹配的字符串的开头:在这种情况下断言失败并且我没有命中。我想要的是相当于 match 如果我在字符串的开头或者 foo
前面有一个非单词字符或表意文字。但我可以'没有正确的咒语来支持这一点。
有什么想法吗?或者这是不可能的?
提前致谢。
I need to emulate the behavior of \b
at the start of a string, where I'm adding additional characters to the set that count as a word boundary. Right now I'm using something like:
"(?<=\\W|\\p{InCJKUnifiedIdeographs})foo"
This works as I would like, unless I'm at the start of the string being matched: in which case the assertion fails and I don't get a hit. What I want is the equivalent of match if I'm at the start of the string or foo
is preceded by a non-word character or an ideograph. But I can't get the right incantation to support that.
Any thoughts? Or is this impossible?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将字符串开头锚点添加到后向条件中即可。
Just add the start-of-string anchor to the lookbehind conditions.