简体   繁体   English

我有正则表达式将字符串拆分为单词,数字和标点符号列表。 如何制作列表中的“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?

It's looks so: 它看起来如此:

string[] lines = Regex.Split(line, @"\s+|(?!^)(?=\p{P})|(?<=\p{P})(?!$)");

It's split "ASds22d. asd ,156" to "ASds22d" + "." + "asd" + "," + "156" 它将"ASds22d. asd ,156"拆分为"ASds22d" + "." + "asd" + "," + "156" "ASds22d" + "." + "asd" + "," + "156" . "ASds22d" + "." + "asd" + "," + "156"

Here is problem with strings like "az", "0-9" or variations like "ac" and "4-5". 这是“az”,“0-9”等字符串或“ac”和“4-5”等变体的问题。 My regex split "az 1-9" to "a" + "-" + "z" + "1" + "-" + "9" but i need just "az" + "1-9" . 我的正则表达式将"az 1-9"分为"az 1-9" "a" + "-" + "z" + "1" + "-" + "9"但我只需要"az" + "1-9"

Can someone fix this regex? 有人能解决这个正则表达式吗?

\s+|(?!^|-)(?=\p{P})|(?<=\p{P})(?<!-)(?!$)

You can try something like this.This will not split on - .If you have any examples where split on - is required it can OR ed again. 您可以尝试这样的事情。这不会分裂- 。如果您有任何需要拆分的示例-需要它可以再次进行OR编辑。

See demo. 见演示。

https://regex101.com/r/iS6jF6/3 https://regex101.com/r/iS6jF6/3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确地将字符串转换为单词和标点符号列表? - How can I properly turn string to list of words and punctuation marks? 有没有办法找出C#中的字符串是否不包含数字和字符0-9和AZ? - Is there a way I can find out if a string in C# does not contain the numbers & characters 0-9 and A-Z? 在 C# 中将字符串从数字 0-9 增加到小写 az 到大写 AZ - Increment a string from numbers 0-9 to lowercase a-z to uppercase A-Z in C# 我当前的问题是我想将字符串分成单个单词和标点符号,但不知道如何 - My current problem is that I want to separate a string into single words and punctuation marks but don't know how 对AZ,0-9和-使用正则表达式和-? - Using regex for A-Z, 0-9, - and dot? 如何获取正则表达式来检查字符串是否只包含字母字符[az]或[AZ]? - How can I get a regex to check that a string only contains alpha characters [a-z] or [A-Z]? 正则表达式分为单词和标点符号 - Regex split into words and punctuation/symbols 如何创建az和0-9(最多8个字符)的字典并将其另存为.txt文件? - How can I create a dictionary with a-z and 0-9 up to 8 characters long and save it as a .txt file? 如何按单词而不是 .?? 拆分文本? 使用 Regex Split 标记? - How to split a text by words instead of .?! marks using Regex Split? 如何使用正则表达式拆分字符串以返回值列表? - How can I split a string using regex to return a list of values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM