1

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"

Dharman
  • 30,962
  • 25
  • 85
  • 135
Developer
  • 3,857
  • 4
  • 37
  • 47

2 Answers2

2

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: "/(?=:):(.*)/": " "
}
m87
  • 4,445
  • 3
  • 16
  • 31
  • Thanks siam, for some reason its not working in smarty. but the below expression removing \n. {assign var="articleTitle" value="Infertility unlikely to\nbe passed on, experts say."} {$articleTitle|regex_replace:"/[\r\t\n]/":" "}I want something to work in smarty. – Developer Feb 23 '17 at 11:26
  • @Developer what you actually trying to replace? the value doesn't even match with the string that you provided in the question – m87 Feb 23 '17 at 11:34
  • I want to print everything before colon, no function seems to work in smarty. except the above one I mentioned in comment. my final output for the string i provided should be "2017 - Alpha Romeo United kingdom" using regex_replace function – Developer Feb 23 '17 at 11:38
  • @Developer is native php is an option for you? – m87 Feb 23 '17 at 11:44
  • its not accepting php, otherwise i would have used simple explode. :( can you use the example above mentioned in my comment to get the output from string – Developer Feb 23 '17 at 11:46
  • @Developer check updated answer – m87 Feb 23 '17 at 11:56
  • Great siam. working like a charm. thanks a ton. you saved my day :) – Developer Feb 23 '17 at 12:02
1
     private void Form1_Load(object sender, EventArgs e)
    {
        string str = "2017 - Alpha Romeo United kingdom : New vehicle (by abc)";
        str = Regex.Replace(str, @":+(.*)", "");
        MessageBox.Show(str);

    }
AVK
  • 72
  • 4