dev*_*per 6 regex search xpath xpath-2.0
使用XPath,我想"匹配整个单词"(用户选项,就像在VS搜索中一样).
似乎函数contains和matches类似的工作虽然匹配允许标志像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"/>
Run Code Online (Sandbox Code Playgroud)
但我想用它matches来返回仅匹配"Cat"全字的结果:
<cat name="Cat" color="grey"/>
Run Code Online (Sandbox Code Playgroud)
如何调整匹配查询以使其与整个单词匹配?
编辑:我忘了提到我还需要使用匹配功能,因为我需要不区分大小写的标志.
使用^和$字符作为锚点怎么样?
//cat[descendant-or-self::*[@*[matches(.,'^Cat$')]]]
Run Code Online (Sandbox Code Playgroud)
来自XQuery 1.0和XPath 2.0中的RegEx语法:
添加了两个元字符^和$.默认情况下,元字符 ^匹配整个字符串的开头,而$匹配整个字符串的结尾.
| 归档时间: |
|
| 查看次数: |
22454 次 |
| 最近记录: |