且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在正则表达式中使用字符串变量?

更新时间:2022-12-17 12:32:53

尝试以下代码。



字符串 str = one] two [three-four;
str = Regex.Replace(str, [^ 0-9a-zA-Z] + - );


您可以尝试使用 Regex.Replace

请参阅 Regex.Replace方法 [ ^ ]



 计划
{
静态 void Main( string [] args)
{
string s1 = one_two;
string s2 = one_two_three跨度>;
string s3 = one_two_three_four跨度>;
string s4 = one] two [三四跨度>;

[更新]
正则表达式regex = 正则表达式( @ [_ \ [\]]);
正则表达式regex = new 正则表达式( @ [\\\\]] );

string result = 跨度>;
result = regex.Replace(s1, new MatchEvaluator(Program.ReplaceWithHyphen));
result = regex.Replace(s2, new MatchEvaluator(Program.ReplaceWithHyphen));
result = regex.Replace(s3, new MatchEvaluator(Program.ReplaceWithHyphen));
result = regex.Replace(s4, new MatchEvaluator(Program.ReplaceWithHyphen));

}

[更新]
静态 string ReplaceWithHyphen(匹配m)
{
// 返回连字符作为替换如果字符不是字母或数字
char ch = m.Value [ 0 ];
if (Char.IsLetterOrDigit(ch))
return ch.ToString( );
else
return - ;
}
}


Regex + Linq解决方案:

列表&lt ;串GT; words =  new 列表< string> {  one_two
one_two_three
one_two_three_four
酮] 2 [3-4跨度>};


var qry = words.Select(w => w = new 正则表达式( @ (\])|(\ [)|(_))。替换(w, - ));

b $ b

结果:

一二
一二三
一-two-three-four
one-two-three-four


searching elements are one, two, three.......twenty

these elements are available in different possible ways.
one_two
one_two_three
one_two_three_four
one]two[three-four

the expected output is
one-two
one-two-three
one-two-three-four
one-two-three-four

it means all special symbol replace by (-) hyphen,
any possible to get answer

Try below code.

string str = "one]two[three-four";
str = Regex.Replace(str, "[^0-9a-zA-Z]+", "-");


You can try to use Regex.Replace
See Regex.Replace Method[^]

class Program
{
    static void Main(string[] args)
    {
        string s1 = "one_two";
        string s2 = "one_two_three";
        string s3 = "one_two_three_four";
        string s4 = "one]two[three-four";

        [UPDATE]
        Regex regex = new Regex(@"[_\[\]]");
        Regex regex = new Regex(@"[\S\s]");

        string result = "";
        result = regex.Replace(s1, new MatchEvaluator(Program.ReplaceWithHyphen));
        result = regex.Replace(s2, new MatchEvaluator(Program.ReplaceWithHyphen));
        result = regex.Replace(s3, new MatchEvaluator(Program.ReplaceWithHyphen));
        result = regex.Replace(s4, new MatchEvaluator(Program.ReplaceWithHyphen));

    }

    [UPDATE]
    static string ReplaceWithHyphen(Match m)
    {
        // Return the hyphen as a replacement if the character is not a letter or digit
        char ch = m.Value[0];
        if (Char.IsLetterOrDigit(ch))
            return ch.ToString();
        else
            return "-";
    }
}


Regex + Linq solution:
List<string> words = new List<string>{"one_two",
            "one_two_three",
            "one_two_three_four",
            "one]two[three-four"};


var qry = words.Select(w=>w = new Regex(@"(\])|(\[)|(_)").Replace(w,"-"));



Result:

one-two
one-two-three
one-two-three-four
one-two-three-four