英文:
Split string using multiple patterns, where second pattern matches smaller parts of the first
问题
Here's the translated code snippet:
for (String substr : "§x§7§3§7§5§f§f§ltest1 §rtest2".split("((?<=(§x(§[0-9a-f]){6}))|(?<=§[0-9a-z])|(?=§[0-9a-z]))")) {
System.out.println(substr);
}
Please note that the code itself remains in English since code keywords and syntax are typically written in English regardless of the programming language used.
英文:
I'm reading special "formatting codes" in a string and am trying to split the string so that I have those formatting codes and the string's text separated.
There are two "types" of formatting codes: "Encoded" hex colors: §x§7§3§7§5§f§f
and other codes in the format of §r
.
Given the example string: §x§7§3§7§5§f§f§ltest1 §rtest2
I need the larger pattern split as a whole, and then the smaller ones. I can do what I want on those patterns separately, but am having trouble combining them into a single regex. Because the second pattern matches pieces of the first pattern, it's just splitting everything into smaller groups.
I'm trying this:
for (String substr : "§x§7§3§7§5§f§f§ltest1 §rtest2".split("((?<=(§x(§[0-9a-f]){6}))|(?<=§[0-9a-z])|(?=§[0-9a-z]))")) {
System.out.println(substr);
}
My expected output is:
§x§7§3§7§5§f§f
§l
test1
§r
test
My actual output is:
§x
§7
§3
§7
§5
§f
§f
§l
test1
§r
test2
When I split the expressions up into different split
tests, they work, they're just not working together.
答案1
得分: 2
Instead of splitting, you could just use this simplified regex for matching:
§x(?:§[0-9a-f]){6}|§[0-9a-z]|[^§\s]+
RegEx Details:
§x(?:§[0-9a-f]){6}
: 匹配以§x
开头的文本,后面跟着 6 个十六进制字符|
: 或§[0-9a-z]
: 匹配以§
开头的文本,后面跟着一个字母数字字符|
: 或[^§\s]+
: 匹配 1 个或多个非空格且非§
字符
Code:
final String regex = ""§x(?:§[0-9a-f]){6}|§[0-9a-z]|[^§\\s]+"";
final String string = ""§x§7§3§7§5§f§f§ltest1 §rtest2"";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
英文:
Instead of splitting, you could just use this simplified regex for matching:
§x(?:§[0-9a-f]){6}|§[0-9a-z]|[^§\s]+
RegEx Details:
§x(?:§[0-9a-f]){6}
: Match text starting with§x
and 6 hex characters|
: OR§[0-9a-z]
: Match text starting with§
and an alphanumeric|
: OR[^§\s]+
: Match 1+ non-whitespace and non-§
characters
Code:
final String regex = "§x(?:§[0-9a-f]){6}|§[0-9a-z]|[^§\\s]+";
final String string = "§x§7§3§7§5§f§f§ltest1 §rtest2";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println( matcher.group(0) );
}
答案2
得分: 1
你可以使用以下正则表达式:
?((?:§[^§])(?=[^§])|[^§ ]{2,})
它的工作原理如下:
?
可选地匹配空格字符。((?:§[^§])(?=[^§])|[^§ ]{2,})
捕获以下之一:(?:§[^§])(?=[^§])
匹配以下内容:(?:§[^§])
匹配§
后跟任何字符,但不包括§
。(?=[^§])
前瞻,确保接下来的字符不是§
(与(?!§)
相同,但更高效)。
[^§ ]{2,}
匹配任何字符,除了§
或空格,两次或更多。
通过替换为 \n$1
结果:
§x§7§3§7§5§f§f
§l
test1
§r
test2
英文:
You can use the following regex:
?((?:§[^§])(?=[^§])|[^§ ]{2,})
How it works:
?
optionally match the space character((?:§[^§])(?=[^§])|[^§ ]{2,})
capture either of the following:(?:§[^§])(?=[^§])
match the following:(?:§[^§])
match§
followed by any character except§
(?=[^§])
lookahead ensuring what follows is not§
(same as(?!§)
but more efficient)
[^§ ]{2,}
match any character except§
or space two or more times
With the substitution of \n$1
Result:
§x§7§3§7§5§f§f
§l
test1
§r
test2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论