java中的正则表达式解析字符串

java中的正则表达式解析字符串,java,regex,Java,Regex,我正在使用Java。我需要使用正则表达式解析以下行: <actions>::=<action><action>|X|<game>|alpha ::=| X | alpha 它应该给我令牌,,X和 什么样的正则表达式有效 我试着做一些类似的事情:”,但这并没有考虑到原始模式中的X或alpha,,不清楚你的意思是,模式中是否存在,我同意这个假设 String pattern="<actions>::=<(.*?)><(

我正在使用Java。我需要使用正则表达式解析以下行:

<actions>::=<action><action>|X|<game>|alpha
::=| X | alpha
它应该给我令牌
X

什么样的正则表达式有效


我试着做一些类似的事情:
,但这并没有考虑到原始模式中的
X
alpha
,,不清楚你的意思是,模式中是否存在,我同意这个假设

String pattern="<actions>::=<(.*?)><(.+?)>\|(.+)\|<(.*?)\|alpha";

String pattern=“::=\\\\\;(.+)\\\\\\\\\\您应该有这样的内容:

String input = "<actions>::=<action><action>|X|<game>|alpha";
Matcher matcher = Pattern.compile("(<[^>]+>)(<[^>]+>)\\|([^|]+)\\|(<[^|]+>)").matcher(input);
while (matcher.find()) {
     System.out.println(matcher.group().replaceAll("\\|", ""));
}
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());
}

您可以尝试以下方法:

String input = "<actions>::=<action><action>|X|<game>|alpha";
Matcher matcher = Pattern.compile("(<[^>]+>)(<[^>]+>)\\|([^|]+)\\|(<[^|]+>)").matcher(input);
while (matcher.find()) {
     System.out.println(matcher.group().replaceAll("\\|", ""));
}
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());
}
String str=“::=| X | | alpha”;
str=str.split(“=”[1];
Pattern=Pattern.compile(“|\\\\\.*?\\\\\\”);
Matcher-Matcher=pattern.Matcher(str);
while(matcher.find()){
System.out.println(matcher.group());
}

您可以使用以下Java正则表达式:

Pattern pattern = Pattern.compile
       ("::=(<[^>]+>)(<[^>]+>)\\|([^|]+)\\|(<[^>]+>)\\|(\\w+)$");
Pattern=Pattern.compile
(“::=(]+>)(]+>)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\(]+>)\\\\\\\\\\\\\\\\\\\\\\\\(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;

是否匹配
alpha
是的,它还应该包括alpha。等等,为什么alpha在这里硬编码。是的,它应该包括“”以及不包含这些“”结尾的单词。在上面的示例中,标记应该是,X,,alpha。这包括X和| X |。正则表达式应该忽略|模式不应该包括“|“。这个吐出:token:token:token:token:| X | token:你能告诉我如何标记这个::=动作吗?”。这里没有“|”,您需要获得代币、、和动作?谢谢。@Dev:或者在这里查看运行上述正则表达式的Java代码: