开发者

Regex for string in C#

开发者 https://www.devze.com 2023-03-27 18:29 出处:网络
I have string on C#: string buf = \"\\r\\n\\r\\n\\r\\n103E\\r\\n\\r\\n\\r\\n\\r\\n\\r\\n\\r\\ntest1\\r\\n\\r\\n\\r\\ntest2\\r\\n\\r\\n\\r\\n\\r\\n\\r\\n开发者_开发百科 \\r\\n\\r\\n\\r\\n\\r\\n&nb

I have string on C#: string buf = "\r\n \r\n \r\n 103E\r\n \r\n \r\n \r\n \r\n \r\ntest1 \r\n \r\n \r\n test2\r\n \r\n \r\n \r\n \r\n 开发者_开发百科 \r\n \r\n \r\n \r\n  x16\r\n \r\n"

I want to get the strings

1. 103E
2. test1
3. test2
4. 16

How to do it using regex.


Try this:

var separator = new string[] { "\r", "\n", " ", " " };
var result = buf.Split(separator, StringSplitOptions.RemoveEmptyEntries);
// result == { "103E", "test1", "test2", "x16" }

Note that, if your input is arbitrary HTML, you should use an HTML parser.


\S+ will match one or more non-whitespace characters. That should get you started.

source: http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号