Java基本正则表达式:获取两个占位符的值
在本例中,有人会键入“奶牛跳过月亮”或 “狐狸跳过狗”或“猫跳过老鼠”。。。 我需要弄清楚他们在两个占位符中输入了什么值。Java基本正则表达式:获取两个占位符的值,java,regex,Java,Regex,在本例中,有人会键入“奶牛跳过月亮”或 “狐狸跳过狗”或“猫跳过老鼠”。。。 我需要弄清楚他们在两个占位符中输入了什么值。 因此,我的问题是如何获得正则表达式中两个[a-z]+点的值。您使用以偏执标记的捕获组:“a-z]+跳过了[a-z]+” 然后使用matcher.group(1)和matcher.group(2)来检索它们(组0始终是整个匹配)。您使用以偏执标记的捕获组:“the([a-z]+)跳过了([a-z]+)” 然后使用matcher.group(1)和matcher.group(2
因此,我的问题是如何获得正则表达式中两个[a-z]+点的值。您使用以偏执标记的捕获组:
“a-z]+跳过了[a-z]+”
然后使用
matcher.group(1)
和matcher.group(2)
来检索它们(组0始终是整个匹配)。您使用以偏执标记的捕获组:“the([a-z]+)跳过了([a-z]+)”
然后使用
matcher.group(1)
和matcher.group(2)
检索它们(组0始终是整个匹配)。您应该使用组。尝试使用此正则表达式:
String input = scanner.nextLine();
Pattern pattern = Pattern.compile("the [a-z]+ jumped over the [a-z]+ ")
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
// how do I print out what jumped over what???
}
然后使用group(int)方法访问它。以下是一个例子:
您应该使用组。尝试使用此正则表达式:
String input = scanner.nextLine();
Pattern pattern = Pattern.compile("the [a-z]+ jumped over the [a-z]+ ")
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
// how do I print out what jumped over what???
}
然后使用group(int)方法访问它。以下是一个例子:
在正则表达式中,使用以下命令捕获组:
"the ([a-z]+) jumped over the ([a-z]+) "
如果regexp匹配,则可以按如下方式获取捕获的s:
Pattern pattern = Pattern.compile("the ([a-z]+) jumped over the ([a-z]+) ");
在正则表达式中,使用以下命令捕获组:
"the ([a-z]+) jumped over the ([a-z]+) "
如果regexp匹配,则可以按如下方式获取捕获的s:
Pattern pattern = Pattern.compile("the ([a-z]+) jumped over the ([a-z]+) ");