加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – XPath:匹配整个单词(使用匹配函数与不区分大小写

发布时间:2020-12-14 06:07:28 所属栏目:百科 来源:网络整理
导读:使用XPath,我想“匹配整个单词”(用户选项,就像在VS搜索中一样). 似乎函数包含和匹配工作类似,虽然匹配允许像i这样的标志不区分大小写. 换句话说,我使用这两个XPath查询得到了相同的结果: pets dog name="Rupert" color="grey"/ dog name="Ralph" color="br
使用XPath,我想“匹配整个单词”(用户选项,就像在VS搜索中一样).

似乎函数包含和匹配工作类似,虽然匹配允许像i这样的标志不区分大小写.

换句话说,我使用这两个XPath查询得到了相同的结果:

<pets>
    <dog name="Rupert" color="grey"/>
    <dog name="Ralph" color="brown"/>
    <cat name="Marvin the Cat" color="white"/>
    <cat name="Garfield the Cat" color="orange"/>
    <cat name="Cat" color="grey"/>
    <cat name="Fluffy" color="black"/>
</pets>

Matches XPath: //cat[descendant-or-self::*[@*[matches(.,'Cat')]]]
    returns:
    <cat name="Marvin the Cat" color="white"/>
    <cat name="Garfield the Cat" color="orange"/>
    <cat name="Cat" color="grey"/>


Contains XPath: //cat[descendant-or-self::*[@*[contains(.,'Cat')]]]
    returns:
    <cat name="Marvin the Cat" color="white"/>
    <cat name="Garfield the Cat" color="orange"/>
    <cat name="Cat" color="grey"/>

但是我想使用匹配来返回仅匹配“Cat”全字的结果:

<cat name="Cat" color="grey"/>

如何调整匹配查询以使其与整个单词匹配?

编辑:
我忘了提到我还需要使用matches函数,因为我需要case insensitivity标志.

解决方法

使用^和$字符作为锚点怎么样?

//cat[descendant-or-self::*[@*[matches(.,'^Cat$')]]]

从RegEx Syntax in XQuery 1.0 and XPath 2.0开始:

Two meta-characters,^ and $ are added. By default,the meta-character
^ matches the start of the entire string,while $ matches the end of the entire string.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读