Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2023-08-21 03:49:20

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,615
GitHub Twitter

[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='&#160;$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

#2 2023-08-21 22:32:39

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,616
Website

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

Offline

#3 2023-08-22 01:18:52

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,104
Website

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

#4 2023-08-22 03:06:46

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,615
GitHub Twitter

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

#5 2023-08-22 05:02:11

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,104
Website

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 &#160; is inserted.


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

#6 2023-08-22 08:19:17

etc
Developer
Registered: 2010-11-11
Posts: 5,087
Website GitHub

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 &#160; 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="&#160;$1" />

Offline

#7 2023-08-22 08:47:48

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,104
Website

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="&#160;$1" />...

Interesting… this also catches ? but not ! and seems to do something odd with this: &#160; – inserted in a code block. Output for that is:

&amp&#160;;#160X&#160;;

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

#8 2023-08-22 12:31:24

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,615
GitHub Twitter

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:

…&nbsp;;)&nbsp;;

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

#9 2023-08-23 00:32:58

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,104
Website

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&#160;://forum.textpattern.com/viewtopic.php&#160;?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

#10 2023-08-23 04:09:18

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,615
GitHub Twitter

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&#160;://forum.textpattern.com/viewtopic.php&#160;?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='&#160;$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

#11 2023-08-23 06:22:42

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,104
Website

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

Board footer

Powered by FluxBB