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

前端之家收集整理的这篇文章主要介绍了正则表达式 – XPath:匹配整个单词(使用匹配函数与不区分大小写的标志)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用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.

猜你在找的正则表达式相关文章