正则表达式拆分字符串但保留分隔符 [英] Regex split string but keep separators
问题描述
我想对某些对象执行 Regex.Split分隔符,但我想保留分隔符.举一个我正在尝试的例子:
I'd like to do a Regex.Split on some separators but I'd like to keep the separators. To give an example of what I'm trying:
"abc[s1]def[s2][s3]ghi" --> "abc", "[s1]", "def", "[s2]", "[s3]", "ghi"
我想出的正则表达式是 new Regex("\[|\]|\]\[")
.但是,这给了我以下内容:
The regular expression I've come up with is new Regex("\[|\]|\]\[")
. However, this gives me the following:
"abc[s1]def[s2][s3]ghi" --> "abc", "s1", "def", "s2", "", "s3", "ghi"
分隔符消失了(考虑到我的正则表达式,这是有道理的).有没有办法编写正则表达式,以便保留分隔符本身?
The separators have disappeared (which makes sense given my regex). Is there a way to write the regex so that the separators themselves are preserved?
推荐答案
Use zero-length maching lookarounds;你想分开
Use zero-length maching lookarounds; you want to split on
(?=[)|(?<=])
也就是说,我们在前面断言文字 [
匹配的任何地方,或者在后面断言文字 ]
匹配的任何地方.
That is, anywhere where we assert a match of a literal [
ahead, or where we assert a match of literal ]
behind.
作为 C# 字符串文字,这是
As a C# string literal, this is
@"(?=[)|(?<=])"
另见
- regular-expressions.info/Lookarounds
- Java split 正在吞噬我的角色. -- 有很多例子
- Java split is eating my characters. -- has many examples
System.out.println(java.util.Arrays.toString(
"abc[s1]def[s2][s3]ghi".split("(?=\[)|(?<=\])")
));
// prints "[abc, [s1], def, [s2], [s3], ghi]"
System.out.println(java.util.Arrays.toString(
"abc;def;ghi;".split("(?<=;)")
));
// prints "[abc;, def;, ghi;]"
System.out.println(java.util.Arrays.toString(
"OhMyGod".split("(?=(?!^)[A-Z])")
));
// prints "[Oh, My, God]"
这篇关于正则表达式拆分字符串但保留分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!