简体   繁体   English

如何使用正则表达式拆分字符串

[英]how to split string using regex

I'd like to split a string with using pattern like this: 我想用这样的模式分割一个字符串:

it starts and ends with '\\n' and contains an arbitrary number of whitespace or '\\n' in between. 它以'\\ n'开头和结尾,并包含任意数量的空格或'\\ n'。

Edit: 编辑:

This input: 这个输入:

string s = "aaa\n    \nbbb\n    \nccc\n   \n   \nddd"; 

should result in an array containing 应该导致包含的数组

aaa
bbb
ccc
ddd

举个例子, string.Split会更清晰。

var vals = s.Split('\n', StringSplitOptions.RemoveEmptyEntries);

For the one example given, the following would accomplish the specified results: 对于给出的一个示例,以下将完成指定的结果:

string str = "aaa\n   \nbbb\n   \nccc\n   \n   \nddd";
string[] result = Regex.Split(str, "\n\\s*");

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

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