简体   繁体   English

需要帮助找出正确的正则表达式模式

[英]Need help in figuring out the right regex pattern

I need to extract substrings from a string: 我需要从字符串中提取子字符串:

Given string: "< If( ( h == v ) ): { [ < j = (i - f) ;>, < k = (g + t) ;> ] }>" I need two substrings: "j = (i - f)" and "k = (g + t)" . 给定字符串: "< If( ( h == v ) ): { [ < j = (i - f) ;>, < k = (g + t) ;> ] }>"我需要两个子字符串: "j = (i - f)""k = (g + t)"

For this I tried user pattern regex. 为此,我尝试了用户模式正则表达式。 Here's my code: 这是我的代码:

Pattern pattern = Pattern.compile("[<*;>]");
Matcher matcher = pattern.matcher(out.get(i).toString());
while (matcher.find())
     {
        B2.add(matcher.group());
      }

out.get(i).toString() is my input string. out.get(i).toString()是我的输入字符串。 B2 is an ArrayList which will contain the two extracted substrings. B2是一个ArrayList,它将包含两个提取的子字符串。

But, after running the above code, the output I am getting is : [<, <, ;, >, <, ;, >, >] . 但是,运行上面的代码后,我得到的输出是: [<, <, ;, >, <, ;, >, >]

My pattern is not working! 我的模式不起作用! Your help is very much appreciated. 非常感激你的帮助。 Thanks in advance! 提前致谢!

You can use the expression <([^<]+);> . 您可以使用表达式<([^<]+);>

This will match things between < and ;> 这将匹配<和;>之间的内容

Pattern pattern = Pattern.compile("<([^<]+);>");
Matcher matcher = pattern.matcher(out.get(i).toString());
while (matcher.find())
     {
        B2.add(matcher.group(1));
      }

You can see the results on regexplanet: http://fiddle.re/5rty6 您可以在regexplanet上查看结果: http ://fiddle.re/5rty6

your [ and ] are causing you problems. 您的[]会给您带来麻烦。 those symbols mean: "match one among the symbols inside of these" If you remove those, you'll get better results. 这些符号的含义是: "match one among the symbols inside of these"如果删除这些"match one among the symbols inside of these" ,将会得到更好的结果。 You'll also have to escape your pointy brackets when you do that. 这样做时,您还必须避开尖括号。

The next step will be to capture the groups. 下一步将是捕获组。 you normally use ( and ) for that. 您通常使用()

You'll also have to worry about nasty artifacts such as that < at the beginning of the string which will mess with your regex. 您还必须担心令人讨厌的工件,例如字符串开头的< ,它将与您的正则表达式混淆。 in order to deal with that, you'll need to exclude those from your regex. 为了解决这个问题,您需要将它们从正则表达式中排除。

You might end up with 你可能最终会

"\<([^<>]*?)\>"

as your regex. 作为您的正则表达式。 Be sure to check the specific java documentation and to escape your \\ for a final result of 确保检查特定的Java文档并转义\\以获取最终结果

"\\<([^<>]*?)\\>"

If you're wanting to next other < and > inside your pointy brackets, regex has a lot of trouble with that kind of thing, and maybe you should try a different method 如果您想在尖括号内使用其他<> ,则regex在这种情况下会遇到很多麻烦,也许您应该尝试其他方法

Here's a sample regex 这是示例正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM