如何使用Regex.Split分割字符串并保留所有分隔符? [英] How to split string with Regex.Split and keep all separators?
问题描述
如何使用Regex.Split分割字符串并保留所有分隔符?
How to split string with Regex.Split and keep all separators?
我有一个字符串:"substring1 delimeter1 substring2",其中delimeter + substring2是地址的一部分.
I have a string:"substring1 delimeter1 substring2" , where delimeter+substring2 is a part of address.
我也有2个或更多的分度符:delim1,delim2的含义是相等的;
Also i have 2 and more delimeters: delim1,delim2 wich are equivalent in meaning;
我想得到这样的字符串数组:
And i want to get string array like this:
arr[0]="subsctring1";
arr[1]="delim1 subsctring2";
或
arr[1]="delim2 subsctring2;
我有一个模式:
addrArr= Regex.Split(inputText, String.Concat("(?<=",delimeter1, "|",delimeter2, ")"), RegexOptions.None);
但是效果不佳.
您能帮我建立一个有效的模式吗?
Can you help me to create a valid pattern to to that?
推荐答案
您只需要具有前瞻性的模式:
You need a pattern with a lookahead only:
\s+(?=delim1|delim2)
\ s +
将匹配1个或多个空格(因为您的字符串包含空格).如果没有空格,请使用 \ s *
(但是您将需要从结果中删除空条目).请参见
The \s+
will match 1 or more whitespaces (since your string contains whitespaces). In case there can be no whitespaces, use \s*
(but then you will need to remove empty entries from the result). See the regex demo. If these delimiters must be whole words, use \b
word boundaries: \s+(?=\b(?:delim1|delim2)\b)
.
在C#中:
addrArr = Regex.Split(inputText, string.Format(@"\s+(?={0})", string.Join("|", delimeters)));
如果定界符可以包含特殊的正则表达式元字符,则需要在定界符
列表上运行 Regex.Escape
.
If the delimiters can contain special regex metacharacters, you will need to run Regex.Escape
on your delimiters
list.
C#演示:
var inputText = "substring1 delim1 substring2 delim2 substr3";
var delimeters = new List<string> { "delim1", "delim2" };
var addrArr = Regex.Split(inputText,
string.Format(@"\s+(?={0})", string.Join("|", delimeters.Select(Regex.Escape))));
Console.WriteLine(string.Join("\n", addrArr));
这篇关于如何使用Regex.Split分割字符串并保留所有分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!