简体   繁体   English

如何将字符串拆分为包含定界符的单词

[英]How to split string into words including delimiters

How can I split a text into words in .NET, returning both the words and their separators and punctuation marks. 如何在.NET中将文本拆分为单词,同时返回单词及其分隔符和标点符号。

Input: 输入:

"Hello world! What's up?"

Output: 'Hello', ' ', 'world', '!', ' ','What's',' ','up','?' 输出:'Hello','','world','!','','What's',','up','?'

(?=[^\w'])|(?<=[^\w'])

You can split by this.See demo. 您可以按此拆分。请参阅演示。

https://regex101.com/r/iJ7bT6/15 https://regex101.com/r/iJ7bT6/15

var output = Regex.Split(input,“([[^ A-Z0-9]]”,RegexOptions.IgnoreSpace);

使用以下正则表达式:

'(?=[^\w'])|(?<=[^\w'])`

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM