Search code examples
regexsmarty

Regex replace from a string ( smarty)


I'm trying to replace a string with empty space("") after a specific character(colon) ":"

example:2017 - Alpha Romeo United kingdom : New vehicle (by abc)

I want out put as "2017 - Alpha Romeo United kingdom"


Solution

  • You could do it using the following regex (using capturing group and positive lookahead) :

    input        >>  2017 - Alpha Romeo United kingdom : New vehicle (by abc)
    regex search >>  (?=:):(.*)
    replace with >>  " "
    output       >>  2017 - Alpha Romeo United kingdom
    

    see demo / explanation

    smarty

    {
        assign
        var = "articleTitle"
        value = "2017 - Alpha Romeo United kingdom : New vehicle (by abc)"
    } {
        $articleTitle | regex_replace: "/(?=:):(.*)/": " "
    }