Regex.Split() 句子到单词,同时保留空格
我正在使用 Regex.Split() 来获取用户输入并将其转换为列表中的单个单词,但目前它删除了它们添加的任何空格,我希望它保留空格。
string[] newInput = Regex.Split(updatedLine, @"\s+");
I'm using Regex.Split()
to take the user input and turn it into individual words in a list but at the moment it removes any spaces they add, I would like it to keep the whitespace.
string[] newInput = Regex.Split(updatedLine, @"\s+");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将为您提供 4 个分割,每个分割保留所有前导空格。
表示从前面有空格的点开始分割。但如果您单独使用它,它将在示例文本上创建 15 个分割,因为每个空格后面都会跟着另一个空格,以防出现重复的空格。
这意味着从前面没有空格字符并且前面有空格的点分割。
如果文本从空格开始,并且您希望在没有文本的情况下在第一个分割中捕获该文本,那么您可以将表达式修改为以下,
这意味着一系列空格之前需要有一个非空格字符或字符串的开头。
This will give you 4 splits each having all the leading spaces preserved.
Means split from the point where there are spaces ahead. But if you use this alone it will create 15 splits on the sample text because every space is followed by another space in case of repeated spaces.
This means split from a point which has non space character before it and it has spaces ahead of it.
If the text starts from a space and you want that to be captured in first split with no text then you can modify the expression to following
Which means series of spaces need to have a non space character before it OR start of the string.
我猜您感兴趣的一些“单词”实际上是可以接受空格的短语。您无法轻松地将空格字符用作短语分隔符和短语本身中允许的字符。尝试使用逗号作为分隔符:
此版本的正则表达式允许在逗号后添加尾随空格:
I'm guessing that some of the "words" you're interested in are actually phrases where spaces are acceptable. You can't easily use the space character as both a phrase delimiter and an allowable character within the phrase itself. Try using a comma for a delimiter instead:
This version of the regex allows trailing spaces after the commas: