regex 正则表达式从字符串替换(smarty)

vohkndzv  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(142)

我尝试在特定字符(冒号)“:”后用空格(“”)替换字符串
示例:2017 - Alpha Romeo United kingdom : New vehicle (by abc)
我想输出为“2017 - Alpha Romeo United kingdom

2exbekwf

2exbekwf1#

您可以使用以下 regex*(使用capturing grouppositive lookahead)*:

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

参见demo / explanation

聪明

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

ne5o7dgx2#

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);

    }

相关问题