英文:
Extract encoded strings based on percentage symbol with Regex and Java
问题
我正在尝试检测/匹配以%
开头的编码字符。
我的正则表达式是([%][2-9|A-F][0-9A-F]{1,2})+
在regexr.com上它有效,并且匹配到了我需要的内容。
我用这些字符串进行测试:caf%C3%A9+100%+noir%C20
和 test%C3%A9+%C3%A0+100%
在我的Java代码中,它只返回第一个组。
String pattern = "([%][2-9|A-F][0-9A-F]{1,2})+";
Matcher matcher = Pattern.compile(pattern ).matcher(input);
if (matcher.find()) {
for (int i = 0; i < matcher.groupCount(); i++) {
System.out.println(matcher.group(i));
}
}
对于caf%C3%A9+100%+noir%C20
的输出是%C3%A9
而不是%C3%A9
+ %C20
。
对于test%C3%A9+%C3%A0+100%
的输出是%C3%A9
而不是%C3%A9
+ %C3%A0
。
英文:
I am trying to to detect/match encoded chars starting with %
.
My Regex is ([%][2-9|A-F][0-9A-F]{1,2})+
On regexr.com it works and it matched what I need.
I used these strings for tests: caf%C3%A9+100%+noir%C20
and test%C3%A9+%C3%A0+100%
In my Java code it is returning only the first group.
String pattern = "([%][2-9|A-F][0-9A-F]{1,2})+";
Matcher matcher = Pattern.compile(pattern ).matcher(input);
if (matcher.find()) {
for (int i = 0; i < matcher.groupCount(); i++) {
System.out.println(matcher.group(i));
}
}
And the output for caf%C3%A9+100%+noir%C20
is %C3%A9
and not %C3%A9
+ %C20
.
For test%C3%A9+%C3%A0+100%
is %C3%A9
and not %C3%A9
+ %C3%A0
答案1
得分: 2
正则表达式您正在使用的过于复杂。另外,您尝试打印所有匹配项的方式不起作用。请尝试以下代码:
String input = "caf%C3%A9+100%+noir%C20";
String pattern = "(?:%[2-9A-F][0-9A-F]{1,2})+";
Matcher matcher = Pattern.compile(pattern).matcher(input);
while (matcher.find()) {
System.out.println(matcher.group());
}
这将打印:
%C3%A9
%C20
英文:
The Regex you are using is overly complicated. Also, the way you are trying to print all the matches doesn't work. Try this:
String input = "caf%C3%A9+100%+noir%C20";
String pattern = "(?:%[2-9A-F][0-9A-F]{1,2})+";
Matcher matcher = Pattern.compile(pattern ).matcher(input);
while (matcher.find()) {
System.out.println(matcher.group());
}
This prints:
%C3%A9
%C20
答案2
得分: 2
根据 @41686d6564 的评论,解决方案是使用 while
循环和 group(0)
:
String pattern = "([%][2-9A-F][0-9A-F]{1,2})+";
Matcher matcher = Pattern.compile(pattern).matcher(input);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
英文:
Based on @41686d6564 comment, the solution is to use a while
loop and group(0)
:
String pattern = "([%][2-9A-F][0-9A-F]{1,2})+";
Matcher matcher = Pattern.compile(pattern).matcher(input);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论