开发者

| as pattern when using split

开发者 https://www.devze.com 2023-02-28 15:58 出处:网络
string input = @\"12.2.2010|7\"; string pattern = @\"|\"; foreach (string result in Regex.Split(input, pattern))
string input = @"12.2.2010|7";   
string pattern = @"|";

foreach (string result in Regex.Split(input, pattern)) 
{
   Console.WriteLine("'{0}'", result);
}

i want to use | as patter but开发者_Go百科 becouse | means or i can't get 12.2.2010. How can i use | like pattern? I try to use ~ but is the same.


Try to escape the | like this

string pattern = @"\|";


In a regular expression, if you want to match a literal |, you need to escape it with a backslash, like so:

string pattern = @"\|";


The character | has a special meaning in a regular expression, which means you have to escape it with a \ like so:

string pattern = @"\|";

Check out the following website for some more information: http://www.regular-expressions.info/reference.html

0

精彩评论

暂无评论...
验证码 换一张
取 消