[英]How can I properly turn string to list of words and punctuation marks?
I have string: IDLIST, ID NAME;
我有字符串:
IDLIST, ID NAME;
. 。 And need to turn it into list:
{"IDLIST", ",", "ID", "NAME", ";"}
. 并且需要将其转换为列表:
{"IDLIST", ",", "ID", "NAME", ";"}
。
It's obvious how to slice string by free spaces, but what to do with punctuation marks? 很明显如何用自由空间对字符串进行切片,但是如何使用标点符号呢?
Words can be any set of letters. 单词可以是任何字母集。 Punctuation marks also can be any.
标点符号也可以是任何符号。
You could use Regex.Split
function like below, 您可以使用
Regex.Split
函数,如下所示,
string value = "IDLIST, ID NAME;";
string[] lines = Regex.Split(value, @"\s+|(?!^)(?=\p{P})|(?<=\p{P})(?!$)");
foreach (string line in lines) {
Console.WriteLine(line);
OR 要么
You could match all the punctuations or the alphabets and then append the matched strings to a list. 您可以匹配所有标点符号或字母,然后将匹配的字符串附加到列表中。
@"\p{P}|\p{L}+"
\\p{P}
matches any kind of punctuation marks and p{L}
matches any kind of letter from any language. \\p{P}
匹配任何类型的标点符号,而p{L}
匹配任何语言的任何字母。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.