Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
[Solved] Failure with a REGEX
Could you tell me please why this REGEX fails (tests: https://regex101.com/r/p3q1wv/1)?
<txp:body trim="/\s?([?|:|;])/g" escape=' $1' />
Thanks in advance.
Last edited by Pat64 (2023-08-22 03:07:13)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Failure with a REGEX
I can’t test it at the moment but could it be that you need the replace
attribute in place of escape
?
TXP Builders – finely-crafted code, design and txp
Online
Re: [Solved] Failure with a REGEX
This works for me (basic test, and after mucho trial and error till I went to double check the docs…):
trim="/\s?([?|:|;])/"
From the docs (look for “trim”)
Use /value/ to trigger regular expression matching
ps – and as jakob says: replace
instead of escape
Last edited by phiw13 (2023-08-22 01:24:04)
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Re: [Solved] Failure with a REGEX
You are absolutely right! How stupid I am… replace
instead of escape
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Failure with a REGEX
Patrick,
Do you have your (trim="/\s?([?|:|;])/g"
) regex actually working, that is, the replacement is actually happening? When I test it, it silently fails – check the source code, no  
is inserted.
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Re: [Solved] Failure with a REGEX
phiw13 wrote #335675:
Do you have your (
trim="/\s?([?|:|;])/g"
) regex actually working, that is, the replacement is actually happening? When I test it, it silently fails – check the source code, no 
is inserted.
There is no g
modifier in PHP regex, hence the pattern /.../g
is considered invalid by txp. This should work:
<txp:body trim="/\s?([?:;])/" replace=" $1" />
Offline
Re: [Solved] Failure with a REGEX
etc wrote #335676:
There is no
g
modifier in PHP regex, hence the pattern/.../g
is considered invalid by txp.
That is what I had somewhere deeply buried in memory, but having seen Patrick playing more with regex, I thought I was wrong. Thanks for confirming.
<txp:body trim="/\s?([?:;])/" replace=" $1" />...
Interesting… this also catches ?
but not !
and seems to do something odd with this:  
– inserted in a code block. Output for that is:
& ;#160X ;
My variant above on Patricks regex works mostly I think, and even extended to deal with ?
and !
. It has a weakness in that it also seem to catch the character when not preceded by a standard white-space (U+0020).
Edited for clarity.
Last edited by phiw13 (2023-08-22 12:13:59)
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Re: [Solved] Failure with a REGEX
Thanks all!
The solution provided by Oleg is perfect… Except when the text contains hyphens followed by a parenthesis before a semicolon. That’s annoying (and I can’t figure out the reason):
…) ;
changed by:
… ;) ;
Edit: Nope! A strange behavior from my local server (MAMP)! All is perfect. Thanks again for your kindly help ;)
Last edited by Pat64 (2023-08-22 13:26:48)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Failure with a REGEX
Pat64 wrote #335678:
All is perfect. Thanks again for your kindly help ;)
Hmm, not really… Try inserting a (Textile) link, like this:
link to "forum post":https://forum.textpattern.com/viewtopic.php?pid=335677#p335677
The actual output:
<a href="https ://forum.textpattern.com/viewtopic.php ?pid=335677#p335677">forum</a>
Which will not be a very useful link… I think ? Inserting images has the same issue.
In both cases the regex not only finds the combination of “space” + punctuation (:
or ?
), but also the punctuation preceded by any character.
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Re: [Solved] Failure with a REGEX
phiw13 wrote #335680:
Hmm, not really… Try inserting a (Textile) link, like this:
link to "forum post":https://forum.textpattern.com/viewtopic.php?pid=335677#p335677...
The actual output:
<a href="https ://forum.textpattern.com/viewtopic.php ?pid=335677#p335677">forum</a>...
Which will not be a very useful link… I think ? Inserting images has the same issue.
In both cases the regex not only finds the combination of “space” + punctuation (
:
or?
), but also the punctuation preceded by any character.
So the solution could be this one:
<txp:body trim="/\s([?!:;])/" replace=' $1' />
Last edited by Pat64 (2023-08-23 04:09:42)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Failure with a REGEX
So the solution could be this one:
Hmm… I can’t break that one at the moment… :-( Looks like a winner!
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Pages: 1