如何在正则表达式中使用字符串变量? [英] How to use the string variables in regex?
本文介绍了如何在正则表达式中使用字符串变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
搜索元素是一,二,三.......二十
这些元素有不同的可用方式。
one_two
one_two_three
one_two_three_four
one] two [three-four
预期产量是
一二
一二三
一 - 两三四
一二三四
这意味着所有特殊符号替换为( - )连字符,
任何可能得到的答案
解决方案
尝试以下代码。
<前lang =cs> 字符串 str = < span class =code-string> 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解决方案:
列表< ;串GT; words = new 列表< string> { one_two,
one_two_three,
one_two_three_four,
酮] 2 [3-4跨度>};
var qry = words.Select(w => w = new 正则表达式( @ (\])|(\ [)|(_))。替换(w, - ));< br $> 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 useRegex.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
这篇关于如何在正则表达式中使用字符串变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文