提取基于百分号的编码字符串,使用正则表达式和Java。

huangapple go评论60阅读模式
英文:

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%C20test%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 = &quot;([%][2-9|A-F][0-9A-F]{1,2})+&quot;;
Matcher matcher = Pattern.compile(pattern ).matcher(input);
if (matcher.find()) {
  for (int i = 0; i &lt; 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 = &quot;caf%C3%A9+100%+noir%C20&quot;;
String pattern = &quot;(?:%[2-9A-F][0-9A-F]{1,2})+&quot;;
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 = &quot;([%][2-9A-F][0-9A-F]{1,2})+&quot;; 
Matcher matcher = Pattern.compile(pattern).matcher(input);
while (matcher.find()) {
  System.out.println(matcher.group(0));
}

huangapple
  • 本文由 发表于 2020年8月11日 20:42:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63358383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定