1

I'm trying to do a regex replace (http://www.smarty.net/docsv2/en/language.modifier.regex.replace.tpl) using smarty but I'm unsure of the correct regex syntax.

What im trying to achieve is turning:

  • i-am-something-specific-thing-1
  • i-am-something-specific-thing-2
  • i-am-something-specific-thing-3
  • i-am-something-specific-thing-4

into i-am-something

Any pointers and explanations as how the regex syntax would look like for this would be much appreciated.

1 Answer 1

2

I'm not really familiar with smarty, but it looks like you want something like this:

{$someVariable|regex_replace:"/-specific-thing-\d+$/":""}

This should remove -specific-thing- followed by any digit, when it appears at the end of a the string.

Update To replace that section of the string in a file name, use:

{$someVariable|regex_replace:"/-specific-thing-\d+\.html$/":".html"}

or this, which will work for any extension:

{$someVariable|regex_replace:"/-specific-thing-\d+(\.\w+)$/":"\\1"}
Sign up to request clarification or add additional context in comments.

4 Comments

Psssst, you forgot specific :)
Many thanks for the answer; I've tried it but it doesn't seem to be working- would it make a different if what I'm replacing is in fact i-am-something-specific-thing-1.html to be i-am-something.html?
Excellent - thanks a bunch! I really must learn regex better - do you know of any good resources?
regular-expressions.info or the PHP manual are good places to start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.