Using XPath, I would like to "Match whole word" (option for user, just like in VS search).
It seems as though the functions contains
and matches
work similarly though matches allows for flags like i
for case insensitivity.
In other words, I am getting the same results with these two XPath queries:
<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"/>
But I would like to use matches
to return results that match "Cat" whole word only:
<cat name="Cat" color="grey"/>
How can I adjust the matches query so it matches whole word?
EDIT: I forgot to mention that I need to still use the matches function because I need the case insensitivity flag.
What about using ^
and $
characters as anchors?
//cat[descendant-or-self::*[@*[matches(.,'^Cat$')]]]
From 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.