Re: Ungready Flag on Regex
| Newsgroups | gmane.text.xml.xsl.general.mulberrytech |
|---|---|
| Message-ID | <[email protected]> |
For sure, regex are strange to me, even if I think they are really usefull. So, when a regex king give me the right regex to do exactly what I want, I take this regex, and I adapt it to other similar use-cases. My first problem was with comments, and I've adapted it to other kind of markers : **bold** $$code$$ @@link@@ The first two are Ok, but I can not make it run for the third case. I join the XSL, a XSpec file, I have one scenario that fails. If someone could : 1. give me the solution 2. explain me why it was failing 3. explain me why the solution works it would be really a good thing. In France, we now have a curfew at 9pm. There a lot of beer that can not be sold ; i may have a barrel for the guy who help me ! Best regards, Christophe Le 08/10/2020 à 16:54, Imsieke, Gerrit, le-tex [email protected] a écrit : > > > On 08.10.2020 16:26, Liam R. E. Quin [email protected] wrote: >> You want [^*]*? > > Thinking of the brilliant potential for misunderstanding that this > answer entails. > > Christophe: I don't understand why you are asking me whether I want > [^*]*. > > Jokes aside, I think you need the non-greedy question mark modifier, > but not on [^*]*. You use it to make .* non-greedy. Otherwise it will > stop at the first '*' that it encounters. > > This works: > > <xsl:template name="xsl:initial-template"> > <doc> > <xsl:analyze-string > select="'Comments are delimited by **/* */** or prefixed by > **//**'" > regex="\*\*(.*?)\*\*"> > <xsl:matching-substring> > <b> > <xsl:value-of select="regex-group(1)"/> > </b> > </xsl:matching-substring> > <xsl:non-matching-substring> > <xsl:value-of select="."/> > </xsl:non-matching-substring> > </xsl:analyze-string> > </doc> > </xsl:template> > --~---------------------------------------------------------------- XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/3329386 or by email: [email protected] --~--
format-text.xsl
(text/xml, 2.1 KB)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:pt="com:oxiane:formation:oxslides:java:text"
exclude-result-prefixes="#all"
expand-text="true"
version="3.0">
<xd:doc scope="stylesheet">
<xd:desc>
<xd:p>Replace the **text** by bold, the $$text$$ by code, and the @@text@@ by link</xd:p>
</xd:desc>
</xd:doc>
<xsl:function name="pt:replaceTextMarkers" as="item()+">
<xsl:param name="s" as="xs:string"/>
<xsl:analyze-string select="$s" regex="\*\*(.*?)\*\*">
<xsl:matching-substring><text:span text:style-name="TB">{pt:replaceTextDollar(substring(.,3, string-length(.)-4))}</text:span></xsl:matching-substring>
<xsl:non-matching-substring>{pt:replaceTextDollar(.)}</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:function>
<xsl:function name="pt:replaceTextDollar" as="item()+">
<xsl:param name="s" as="xs:string"/>
<xsl:analyze-string select="$s" regex="\$\$(.*?)\$\$">
<xsl:matching-substring><text:span text:style-name="TCode">{pt:replaceTextArobase(substring(.,3, string-length(.)-4))}</text:span></xsl:matching-substring>
<xsl:non-matching-substring>{pt:replaceTextArobase(.)}</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:function>
<xsl:function name="pt:replaceTextArobase" as="item()+">
<xsl:param name="s" as="xs:string"/>
<xsl:analyze-string select="$s" regex="@@(.*?)@@">
<xsl:matching-substring><text:span text:style-name="TLink">{substring(.,3, string-length(.)-4)}</text:span></xsl:matching-substring>
<xsl:non-matching-substring>{.}</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:function>
<xsl:function name="pt:unescapeSpecialChars" as="xs:string">
<xsl:param name="s" as="xs:string"/>
<xsl:sequence select="$s=>replace('\*', '*', 'q')=>replace('\$', '$', 'q')=>replace('\@','@', 'q')"/>
</xsl:function>
</xsl:stylesheet>
format-text.xspec
(text/xml, 4.7 KB)
<?xml version="1.0" encoding="UTF-8"?>
<x:description
xmlns:x="http://www.jenitennison.com/xslt/xspec"
xmlns:pt="com:oxiane:formation:oxslides:java:text"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
stylesheet="format-text.xsl"
xslt-version="3.0">
<x:scenario label="Testing @@ links">
<x:scenario label="no marker">
<x:call function="pt:replaceTextArobase">
<x:param>content</x:param>
</x:call>
<x:expect label="content">content</x:expect>
</x:scenario>
<x:scenario label="totally marked">
<x:call function="pt:replaceTextArobase">
<x:param>@@http://www.google.com@@</x:param>
</x:call>
<x:expect label="link around"><text:span text:style-name="TLink">http://www.google.com</text:span></x:expect>
</x:scenario>
<x:scenario label="link with @ inside">
<x:call function="pt:replaceTextArobase">
<x:param>@@mailto:[email protected]@@</x:param>
</x:call>
<x:expect label="link around"><text:span text:style-name="TLink">mailto:[email protected]</text:span></x:expect>
</x:scenario>
<x:scenario label="two sequences">
<x:call function="pt:replaceTextMarkers">
<x:param>outside @@inside first@@ again outside @@inside second@@ final</x:param>
</x:call>
<x:expect label="only two links">outside <text:span text:style-name="TLink">inside first</text:span> again outside <text:span text:style-name="TLink">inside second</text:span> final</x:expect>
</x:scenario>
</x:scenario>
<x:scenario label="testing bold">
<x:scenario label="no marker">
<x:call function="pt:replaceTextMarkers">
<x:param>content</x:param>
</x:call>
<x:expect label="content">content</x:expect>
</x:scenario>
<x:scenario label="totally marked">
<x:call function="pt:replaceTextMarkers">
<x:param>**This is bold**</x:param>
</x:call>
<x:expect label="Bolded"><text:span text:style-name="TB">This is bold</text:span></x:expect>
</x:scenario>
<x:scenario label="star inside bold">
<x:call function="pt:replaceTextMarkers">
<x:param>normal **bold with * in** outside</x:param>
</x:call>
<x:expect label="star is bold">normal <text:span text:style-name="TB">bold with * in</text:span> outside</x:expect>
</x:scenario>
<x:scenario label="two bolds">
<x:call function="pt:replaceTextMarkers">
<x:param>if **bold1** then **bold2** else nothing</x:param>
</x:call>
<x:expect label="two bolds">if <text:span text:style-name="TB">bold1</text:span> then <text:span text:style-name="TB">bold2</text:span> else nothing</x:expect>
</x:scenario>
</x:scenario>
<x:scenario label="testing code">
<x:scenario label="no marker">
<x:call function="pt:replaceTextDollar">
<x:param>content</x:param>
</x:call>
<x:expect label="content">content</x:expect>
</x:scenario>
<x:scenario label="totally code">
<x:call function="pt:replaceTextDollar">
<x:param>$$this is code$$</x:param>
</x:call>
<x:expect label="all code"><text:span text:style-name="TCode">this is code</text:span></x:expect>
</x:scenario>
<x:scenario label="code in non-code">
<x:call function="pt:replaceTextDollar">
<x:param>nowhere $$in code$$ nowhere</x:param>
</x:call>
<x:expect label="code is in the middle of nowhere">nowhere <text:span text:style-name="TCode">in code</text:span> nowhere</x:expect>
</x:scenario>
<x:scenario label="code with $ in">
<x:call function="pt:replaceTextDollar">
<x:param>outside $$in with $ sign$$ outside</x:param>
</x:call>
<x:expect label="the $ sign is in code">outside <text:span text:style-name="TCode">in with $ sign</text:span> outside</x:expect>
</x:scenario>
</x:scenario>
<x:scenario label="testing unescape">
<x:scenario label="unescape \*">
<x:call function="pt:unescapeSpecialChars">
<x:param>\*foo * bar\*</x:param>
</x:call>
<x:expect label="*foo * bar*" select="'*foo * bar*'"/>
</x:scenario>
<x:scenario label="unescape \$">
<x:call function="pt:unescapeSpecialChars">
<x:param>\$foo $ bar\$</x:param>
</x:call>
<x:expect label="$foo $ bar$" select="'$foo $ bar$'"/>
</x:scenario>
<x:scenario label="unescape \@">
<x:call function="pt:unescapeSpecialChars">
<x:param>\@foo @ bar\@</x:param>
</x:call>
<x:expect label="@foo @ bar@" select="'@foo @ bar@'"/>
</x:scenario>
<x:scenario label="mixed">
<x:call function="pt:unescapeSpecialChars">
<x:param>\*start \@at \$dollar</x:param>
</x:call>
<x:expect label="*start @at $dollar" select="'*start @at $dollar'"/>
</x:scenario>
</x:scenario>
</x:description>