Java正则表达式缺少匹配项
我试图返回一个正则表达式匹配字符串。具体来说,尝试返回字符串中找到的所有元音(aeiou)。下面的代码返回2个oo,但不返回e。 预期:eoo 得到:哦 为什么它没有找到并将e附加到StringBuilder对象?多谢各位Java正则表达式缺少匹配项,java,Java,我试图返回一个正则表达式匹配字符串。具体来说,尝试返回字符串中找到的所有元音(aeiou)。下面的代码返回2个oo,但不返回e。 预期:eoo 得到:哦 为什么它没有找到并将e附加到StringBuilder对象?多谢各位 import java.lang.StringBuilder; import java.util.regex.Matcher; import java.util.regex.Pattern; class Main { public static void main(St
import java.lang.StringBuilder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Main {
public static void main(String[] args) {
String inp = "Hello World!";
System.out.println(vowelOnly(inp));
}
public static String vowelOnly(String input) {
Pattern vowelPattern = Pattern.compile("[aeiou]+");
Matcher vowelMatcher = vowelPattern.matcher(input);
StringBuilder sb = new StringBuilder();
int i = 0;
if (vowelMatcher.find()) {
while (vowelMatcher.find( )) {
sb.append(vowelMatcher.group());
i++;
}
return sb.toString();
} else {
return "No matches";
}
}
}
if语句中对
vouelmatcher.find()
的第一次调用会找到“e”,而while循环中对vouelmatcher.find()
的后续调用会找到所有后续元音。if语句中对vouelmatcher.find()的第一次调用会找到“e”,随后对vouelmatcher.find()的调用会找到
在while循环中查找所有后续元音。在条件中调用元音匹配器.find()
时,告诉匹配器查找与指定模式匹配的第一个字符串。在本例中,它是“e”。当您在while
条件下再次调用它时,匹配器会找到下一个匹配项,在本例中为“o”
从那里,它循环遍历字符串的其余部分。如果您给它输入“Hello World eeee”,它将返回“ooeeee”,因为您总是通过调用.find()
放弃第一个匹配项,而不立即调用.group()
将循环更改为这样,它应该可以工作:
int i = 0;
while (vowelMatcher.find()) {
sb.append(vowelMatcher.group());
i++;
}
return i == 0 ? "No matches" : sb.toString(); // return "no matches" if i is 0, otherwise, string
在if
条件中调用vouelmatcher.find()
时,告诉匹配器找到与指定模式匹配的第一个字符串。在本例中,它是“e”。当您在while
条件下再次调用它时,匹配器会找到下一个匹配项,在本例中为“o”
从那里,它循环遍历字符串的其余部分。如果您给它输入“Hello World eeee”,它将返回“ooeeee”,因为您总是通过调用.find()
放弃第一个匹配项,而不立即调用.group()
将循环更改为这样,它应该可以工作:
int i = 0;
while (vowelMatcher.find()) {
sb.append(vowelMatcher.group());
i++;
}
return i == 0 ? "No matches" : sb.toString(); // return "no matches" if i is 0, otherwise, string
这就是你需要的:
public static void main(String[] args) {
String inp = "Hello World!";
System.out.println(vowelOnly(inp));
}
public static String vowelOnly(String input) {
Pattern vowelPattern = Pattern.compile("[aeiou]+");
Matcher vowelMatcher = vowelPattern.matcher(input);
StringBuilder sb = new StringBuilder();
int i = 0;
while (vowelMatcher.find()) {
sb.append(vowelMatcher.group());
i++;
}
return i == 0 ? "No matches" : sb.toString();
}
这就是你需要的:
public static void main(String[] args) {
String inp = "Hello World!";
System.out.println(vowelOnly(inp));
}
public static String vowelOnly(String input) {
Pattern vowelPattern = Pattern.compile("[aeiou]+");
Matcher vowelMatcher = vowelPattern.matcher(input);
StringBuilder sb = new StringBuilder();
int i = 0;
while (vowelMatcher.find()) {
sb.append(vowelMatcher.group());
i++;
}
return i == 0 ? "No matches" : sb.toString();
}
这可能就是您正在寻找的解决方案:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String inp = "Hello World!";
System.out.println(vowelOnly(inp));
}
public static String vowelOnly(String input) {
Pattern vowelPattern = Pattern.compile("[aeiou]+");
Matcher vowelMatcher = vowelPattern.matcher(input);
StringBuilder sb = new StringBuilder();
while (vowelMatcher.find()) {
sb.append(vowelMatcher.group());
}
if (sb.length() == 0) {
return "No matches";
}
return sb.toString();
}
}
几点注意:
- 您不需要导入
java.lang.*
类
- 使用matcher,无论您找到什么,您都应该立即对其进行分组以收集
- 您不需要迭代变量
StringBuilder.length()
只会显示它是否为空
这可能是您正在寻找的解决方案:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String inp = "Hello World!";
System.out.println(vowelOnly(inp));
}
public static String vowelOnly(String input) {
Pattern vowelPattern = Pattern.compile("[aeiou]+");
Matcher vowelMatcher = vowelPattern.matcher(input);
StringBuilder sb = new StringBuilder();
while (vowelMatcher.find()) {
sb.append(vowelMatcher.group());
}
if (sb.length() == 0) {
return "No matches";
}
return sb.toString();
}
}
几点注意:
- 您不需要导入
java.lang.*
类
- 使用matcher,无论您找到什么,您都应该立即对其进行分组以收集
- 您不需要迭代变量
StringBuilder.length()
只会显示它是否为空
您没有考虑第一个find
的结果,即if(vouelmatcher.find())
中的一个-删除if
中的,如果您确实需要“No matches”
返回,请在循环后测试sb
(我宁愿只返回空字符串,因为这是方法找到的字符串)[消息,如果需要,由调用方创建])如果这是针对大学项目,则返回“无匹配项”可以接受。对于几乎所有其他上下文,最好返回空的字符串
,可选
,或引发异常,但在这种情况下,使用可选
或引发异常可能是过火了。您也可以只添加sb.append(…group())
紧接着就是if
,但是代码重复并没有那么好。您没有考虑第一个查找
,即if(vouelmatcher.find())
中的一个if
的结果。如果您确实需要返回“No matches”
,请在循环后测试sb
(我更喜欢只返回空字符串,因为这是方法找到的[message,如果需要,由调用者创建]),如果这是针对大学项目的,则返回“No matches”可以接受。对于几乎所有其他上下文,最好返回空的字符串
,可选
,或引发异常,但在这种情况下,使用可选
或引发异常可能是过火了。您也可以只添加sb.append(…group())
紧接着就是如果
,但是代码重复并没有那么好