xsl-list
[Top] [All Lists]

Re: [xsl] XSLT Regex for Matching Curly Braces

2019-06-11 12:26:07
On Mon, 2019-06-10 at 18:21 +0000, Don Smith
dsmith_lockesmith(_at_)yahoo(_dot_)com wrote:
 Thank you all,
I used Gerrit's suggestion <xsl:analyze-string select="."
regex="\{{([^}}]+)\}}"> and it appears to work exactly as desired. 

As an addendum, i'll add that i tend to use [{] and [}] and the same
for [(] and [)] so as not to have to worry about regular expression
engines in which those are or aren't special -- in some, \( \) are for
capturing and \{ \} are for counting, for example.

So i end up with
  [{](^{}]+)[}]

This is marginally less efficient than \{ and \} but that seldom
matters in practice.

I used [^{}] partly to avoid nested braces and partly because then the
automatic brace matching in my editor will show the close one matching
with the open one, and that’s a feature that tends to reduce errors.

Sometimes i use variables for bra and ket in XSLT, e.g. if i’m making
CSS or JavaScript or something.

But that's a separate issue from embedding them in XSLT and dealing
with { } being for attribute value templates :) and Mike Kay’s solution
is (unsurprisingly) the best for that.

For a really complex expression, use the x flag:

<xsl:variable as="xs:string" name="within-braces">
    [{]      <!--* opening bace *-->
    (        <!--* $1 *-->
      [^{}]+ <!--* not containing a brace *-->
    )        <!--* end $1 *-->
    [}]      <!--* closing brace *--> 
</xsl:variable>

and then fn:replace($input, $within-braces, 'x'), or more likely,
  <xsl:analyze-string regex="$within-braces" flags="x" . .. . >


Liam


-- 
Liam Quin, https://www.delightfulcomputing.com/
Available for XML/Document/Information Architecture/XSLT/
XSL/XQuery/Web/Text Processing/A11Y training, work & consulting.
Barefoot Web-slave for http://www.fromoldbooks.org/
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>