Smarty regex_replace: how to find one string after match

97 Views Asked by At

We are trying to use regex_replace in tpl file:

{$item->order->order->notes_priv|regex_replace:"/.+?(?=MATCH)/":""}

But this leaves all strings after "MATCH" not the only first one after "MATCH".

We have this text and would like to input only one string after word MATCH:

foo bar foo bar MATCH 12 foo bar
foo bar foo bar MATCH 24,00 foo bar

How we can make input?

12
24,00

We tried many solutions but without success.

1

There are 1 best solutions below

1
Wiktor Stribiżew On

You can use

regex_replace:'/.*MATCH\s+(\S+).*/':'$1'

See the regex demo.

The main idea is to match the whole string, capture just a part and replace with a backreference to the captured group value.

Details:

  • .* - any zero or more chars other than line break chars as many as possible
  • MATCH - a literal string
  • \s+ - one or more whitespaces
  • (\S+) - Group 1 ($1 in the replacement pattern): any one or more non-whitespace chars
  • .* - any zero or more chars other than line break chars as many as possible.