How to find a word within text using XSLT 2.0 and REGEX (which doesn't have \b word boundary)?

StackOverflow https://stackoverflow.com/questions/2397574

문제

I am attempting to scan a string of words and look for the presence of a particular word(case insensitive) in an XSLT 2.0 stylesheet using REGEX.

I have a list of words that I wish to iterate over and determine whether or not they exist within a given string.

I want to match on a word anywhere within the given text, but I do not want to match within a word (i.e. A search for foo should not match on "food" and a search for bar should not match on "rebar").

XSLT 2.0 REGEX does not have a word boundary(\b), so I need to replicate it as best I can.

도움이 되었습니까?

해결책

You can use alternation to avoid repetition:

<xsl:if test="matches($prose, concat('(^|\W)', $word, '($|\W)'),'i')">

다른 팁

If your XSLT 2.0 processor is Saxon 9 then you can use Java regular expression syntax (including \b) with the functions matches, tokenize and replace by starting the flag attribute with an exclamation mark:

<xsl:value-of select="matches('all foo is bar', '\bfoo\b', '!i')"/>

Michael Kay mentioned that option recently on the XSL mailing list.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top