1

I have this regex_replace in my template :

{$product.unit_price_full|regex_replace:"/^[\d\s]*,?\d{2}\s*[€$₪]*\s*/u":""}

the $product.unit_price_full returns me "1,12 $ L" and my regex_replace "/^[\d\s]*,?\d{2}\s*[€$₪]*\s*/" : " " is supposed to remove everything before the "L" but it still display everything like the regex didn't worked, but I try it on regex101 it works perfectly.

If I add the unicode markup, it just removes the dollar symbol but nothing else

This is the output I get 1,00  2.21

So I have to capture the space markup in my regex rule?

Richard
  • 994
  • 7
  • 26
  • Try `/^[\d\s]*,?\d{2}\s*[€$₪]*\s*/u` – Wiktor Stribiżew Jun 05 '19 at 13:12
  • that just removes the $ symbol and nothing else. (I have been read the article you just marked for duplicated, I posted this one because it was not the solution apparently :( please remove the flag if you're ok) – Richard Jun 05 '19 at 13:27
  • Are you sure it is at the string start? Try removing `^`. Also, try single quotes, `'/^[\d\s]*,?\d{2}\s*[€$₪]*\s*/u'` – Wiktor Stribiżew Jun 05 '19 at 13:29
  • I just tried, still everything display but the dollar symbol, for instance I edit my post to show the output I get – Richard Jun 05 '19 at 13:31
  • I just saw that I have an ` ` in my output :\ so I guess I found where the problem was, should I answer myself or not? @WiktorStribiżew – Richard Jun 05 '19 at 13:34
  • If that space is actually a literal hard space, `\s` with `u` modifier must match it. Do you encode the string before passing to regex? You should not. – Wiktor Stribiżew Jun 05 '19 at 13:35
  • Ok! My first regex worked with your suggestion of adding single quotes instead of doubles o_o put that as answer so I mark your answer as solution. Thanks alot – Richard Jun 05 '19 at 13:36
  • Yes, I removed that comment. – Wiktor Stribiżew Jun 05 '19 at 13:40

1 Answers1

1

I suggest the following changes:

  • $ inside a double quoted string literal is used to interpolate string literals and should be escaped with a single backslash if you need to make it a literal $ char. Else, and it is recommended, just use single quotes
  • As you have Unicode chars out of ASCII range in the pattern pass u modifier to the regex so that the string pattern and the input string could be correctly parsed with PCRE.

Use

{$product.unit_price_full|regex_replace:'/^[\d\s]*,?\d{2}\s*[€$₪]*\s*/u':""}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563