[英]Regex split a multiline string using \
I have tried approximately every possible combination of RegexOptions.MultiLine and escaped backslashes in order to split a text using \\ as a separator. 我已经尝试过RegexOptions.MultiLine的所有可能组合和转义的反斜杠,以便使用\\作为分隔符分割文本。
I have this text: 我有这段文字:
The quick brown
Fox jumps\
Over the
Lazy dog\
I want to split it into 我想把它分成
The quick brown
Fox jumps\
and 和
Over the
Lazy dog\
I have tried so far (together with a call to the Split method of the Regex): 到目前为止,我已经尝试过(连同对Regex的Split方法的调用):
Regex regexSplit = new Regex(@"\\$", RegexOptions.Multiline);
Regex regexSplit = new Regex(@"\$", RegexOptions.Multiline);
Regex regexSplit = new Regex(@"\\$", RegexOptions.Singleline);
Regex regexSplit = new Regex(@"\$", RegexOptions.Singleline);
Regex regexSplit = new Regex(@"\\$");
Regex regexSplit = new Regex(@"\$");
Every time I get back the complete original string. 每次我取回完整的原始字符串。 Could you give me a hand please? 你可以帮我一把吗?
Thank you in advance. 先感谢您。
EDIT: I removed an extra space. 编辑:我删除了多余的空间。 The reason why I need to use a Regex is because a \\ might be inside a match enclosed in "" or ''. 之所以需要使用正则表达式,是因为\\可能位于“”或“”内的匹配项内。 This is why I need to match on end of line as well. 这就是为什么我也需要在行尾匹配的原因。
I must add that \\\\$
works when I test the expression using RegexBuddy and the same input text. 当我使用RegexBuddy和相同的输入文本测试表达式时,我必须添加\\\\$
可以工作。
You have an extra space at "Fox jumps\\ " so
@"\\\\$"
won't match.
您在“ Fox jumps \\”处有多余的空间,因此
@"\\\\$"
不匹配。
Either remove the space or use
@"\\\\"
to split.
删除空格或使用
@"\\\\"
进行拆分。
You can also check for spaces
@"\\\\\\s*$"
.
您也可以检查空格
@"\\\\\\s*$"
。
This one should do the trick : 这应该可以解决问题:
var results = Regex.Split(subject, @"\\\s*$", RegexOptions.Multiline);
Why not this simple string split: 为什么不分割这个简单的字符串:
string s = "The quick brown\r\nFox jumps\\\\r\n Over the\r\nLazy dog\\";
s.Split(new string[] { "\\\r\n" }, StringSplitOptions.RemoveEmptyEntries);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.