简体   繁体   English

如何正确地将字符串转换为单词和标点符号列表?

[英]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);

DEMO 演示

OR 要么

You could match all the punctuations or the alphabets and then append the matched strings to a list. 您可以匹配所有标点符号或字母,然后将匹配的字符串附加到列表中。

@"\p{P}|\p{L}+"

DEMO 演示

\\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.

相关问题 我有正则表达式将字符串拆分为单词,数字和标点符号列表。 如何制作列表中的“az”和“0-9”单个元素? - I have regex to split string to words, numbers and punctuation marks list. How to make “a-z” and “0-9” single elements of list? 我当前的问题是我想将字符串分成单个单词和标点符号,但不知道如何 - My current problem is that I want to separate a string into single words and punctuation marks but don't know how 如何仅允许字符串中的第一个标点符号在单词之间具有不同的标记序列 - How to allow only first punctuation mark in string with different marks sequence between words 如何从字符串中删除标点符号? - How can I strip punctuation from a string? PigLatin如何从字符串中去除标点符号? 然后加回来? - PigLatin how can I strip punctuation from a string? And Then add it back? 反转字符串中的单词,标点符号除外 - Reverse words in a string, except punctuation 用逗号分割字符串,忽略引号中的任何标点符号(包括&#39;,&#39;) - Split string by commas ignoring any punctuation marks (including ',') in quotation marks 如何使用列表正确创建分页 <IGrouping<string,Product> &gt;? - How can i create pagination properly with List<IGrouping<string,Product>>? 如何将这个字符串转换为日期时间 - How can I turn this string into a datetime 如何避免字符串中出现单引号 - How can I avoid single quotation marks from my string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM