字符类中的正则表达式锚点 [英] Regex anchors inside character class

查看:51
本文介绍了字符类中的正则表达式锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在字符类中使用锚点?这不起作用:

Is it possible to use anchors inside a character class? This doesn't work:

analyze-string('abcd', '[\s^]abcd[\s$]') 

看起来 ^$ 在字符类中被视为文字;但是,转义它们 (\^, \$) 也不起作用.

It looks like ^ and $ are treated as literal when inside a character class; however, escaping them (\^, \$) doesn't work either.

我正在尝试使用这个表达式来创建单词边界(\b 在 XSLT/XQuery 中不可用),但我不想使用组((^|\s)) -- 因为非捕获组不可用,这意味着在某些情况下,我可能最终会得到大量不需要的捕获组,这会创建一个新的任务来寻找真正的"捕获一组不需要的组.

I'm trying to use this expression to create word boundaries (\b is not available in XSLT/XQuery), but I would prefer not to use groups ((^|\s)) -- since non-capturing groups aren't available, that means in some scenarios I may end up with a large amount of unneeded capture groups, and that creates a new task of finding the "real" capture groups in the set of unneeded ones.

推荐答案

我相信答案是否定的,您不能将 ^$ 作为锚点包含在[],仅作为文字字符.(我希望你以前也能这样做.)

I believe the answer is no, you can't include ^ and $ as anchors in a [], only as literal characters. (I've wished you could do that before too.)

但是,您可以在字符串的前后连接一个空格,然后只需将 \s 查找为单词边界,而不必考虑锚点.例如

However, you could concat a space on the front and back of the string, then just look for \s as word boundaries and never mind the anchors. E.g.

analyze-string(concat(' ', 'abcd xyz abcd', ' '), '\sabcd\s')

您可能还需要在每个 \s 后面加上 +,但这是一个单独的问题.

You may also want + after each \s, but that's a separate issue.

这篇关于字符类中的正则表达式锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆