java java中的正则表达式解析字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15264114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex parse string in java
提问by Dev
I am using Java. I need to parse the following line using regex :
我正在使用 Java。我需要使用正则表达式解析以下行:
<actions>::=<action><action>|X|<game>|alpha
It should give me tokens <action>
, <action>
,X
and <game>
这应该给我令牌<action>
,<action>
,X
和<game>
What kind of regex will work?
什么样的正则表达式会起作用?
I was trying sth like: "<[a-zA-Z]>"
but that doesn't take care of X
or alpha
.
我正在尝试诸如此类的"<[a-zA-Z]>"
事情:但这并不能解决X
or alpha
。
回答by jitendra
You can try something like this:
你可以尝试这样的事情:
String str="<actions>::=<action><action>|X|<game>|alpha";
str=str.split("=")[1];
Pattern pattern = Pattern.compile("<.*?>|\|.*?\|");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
回答by Maroun
You should have something like this:
你应该有这样的事情:
String input = "<actions>::=<action><action>|X|<game>|alpha";
Matcher matcher = Pattern.compile("(<[^>]+>)(<[^>]+>)\|([^|]+)\|(<[^|]+>)").matcher(input);
while (matcher.find()) {
System.out.println(matcher.group().replaceAll("\|", ""));
}
You didn't specefied if you want to return alphaor not, in this case, it doesn't return it.
您没有指定是否要返回alpha,在这种情况下,它不会返回它。
You can return alpha by adding |\\w*
to the end of the regex I wrote.
您可以通过添加|\\w*
到我编写的正则表达式的末尾来返回 alpha 。
This will return:
这将返回:
<action><action>X<game>
回答by javadba
From the original pattern it is not clear if you mean that literally there are <> in the pattern or not, i'll go with that assumption.
从原始模式中,不清楚您的意思是模式中是否真的有 <> ,我将采用该假设。
String pattern="<actions>::=<(.*?)><(.+?)>\|(.+)\|<(.*?)\|alpha";
For the java code you can use Pattern and Matcher: here is the basic idea:
对于 Java 代码,您可以使用 Pattern 和 Matcher:这是基本思想:
Pattern p = Pattern.compile(pattern, Pattern.DOTALL|Pattern.MULTILINE);
Matcher m = p.matcher(text);
m.find();
for (int g = 1; g <= m.groupCount(); g++) {
// use your four groups here..
}
回答by anubhava
You can use following Java regex:
您可以使用以下 Java 正则表达式:
Pattern pattern = Pattern.compile
("::=(<[^>]+>)(<[^>]+>)\|([^|]+)\|(<[^>]+>)\|(\w+)$");