Java正则表达式字符串匹配
我需要帮助。我正在写一个方法,如果Java正则表达式字符串匹配,java,regex,Java,Regex,我需要帮助。我正在写一个方法,如果1954是字符串images/deal/129277/1954 bonus.jpg,它将返回true。我可以使用string.contains,但它并不总是准确的。相反,如果1954年是在确切的位置,我希望它能返回真值。下面的sourceKey是images/deal/129277/1954 bonus.jpg,oldImageId是1954 下面的代码不工作 private boolean keyMatches(String sourceKey, String
1954
是字符串images/deal/129277/1954 bonus.jpg
,它将返回true。我可以使用string.contains,但它并不总是准确的。相反,如果1954年是在确切的位置,我希望它能返回真值。下面的sourceKey是images/deal/129277/1954 bonus.jpg
,oldImageId是1954
下面的代码不工作
private boolean keyMatches(String sourceKey, String oldImageId){
Pattern pattern = Pattern.compile("(.*?)/(\\d+)/(\\d+)-(.*)");
Matcher matcher = pattern.matcher(sourceKey);
return oldImageId.equals(matcher.group(3));
}
试着这样做:
public static void main(String[] args) {
String s = "images/deal/129277/1954-bonus.jpg";
String s1 = "images/deal/1954/1911254-bonus.jpg";
System.out.println(s.matches(".*/1954\\-.*"));
System.out.println(s1.matches(".*/1954\\-.*"));
}
O/p:
试着这样做:
public static void main(String[] args) {
String s = "images/deal/129277/1954-bonus.jpg";
String s1 = "images/deal/1954/1911254-bonus.jpg";
System.out.println(s.matches(".*/1954\\-.*"));
System.out.println(s1.matches(".*/1954\\-.*"));
}
O/p:
试着这样做:
public static void main(String[] args) {
String s = "images/deal/129277/1954-bonus.jpg";
String s1 = "images/deal/1954/1911254-bonus.jpg";
System.out.println(s.matches(".*/1954\\-.*"));
System.out.println(s1.matches(".*/1954\\-.*"));
}
O/p:
试着这样做:
public static void main(String[] args) {
String s = "images/deal/129277/1954-bonus.jpg";
String s1 = "images/deal/1954/1911254-bonus.jpg";
System.out.println(s.matches(".*/1954\\-.*"));
System.out.println(s1.matches(".*/1954\\-.*"));
}
O/p:
看起来你想要这样的东西
String s = "images/deal/129277/1954-bonus.jpg";
String oldImageId = "1954";
Matcher m = Pattern.compile("(.*?)/(\\d+)/(\\d+)-(.*)").matcher(s);
if(m.find())
{
System.out.println(oldImageId.matches(m.group(3)));
}
输出:
true
看起来你想要这样的东西
String s = "images/deal/129277/1954-bonus.jpg";
String oldImageId = "1954";
Matcher m = Pattern.compile("(.*?)/(\\d+)/(\\d+)-(.*)").matcher(s);
if(m.find())
{
System.out.println(oldImageId.matches(m.group(3)));
}
输出:
true
看起来你想要这样的东西
String s = "images/deal/129277/1954-bonus.jpg";
String oldImageId = "1954";
Matcher m = Pattern.compile("(.*?)/(\\d+)/(\\d+)-(.*)").matcher(s);
if(m.find())
{
System.out.println(oldImageId.matches(m.group(3)));
}
输出:
true
看起来你想要这样的东西
String s = "images/deal/129277/1954-bonus.jpg";
String oldImageId = "1954";
Matcher m = Pattern.compile("(.*?)/(\\d+)/(\\d+)-(.*)").matcher(s);
if(m.find())
{
System.out.println(oldImageId.matches(m.group(3)));
}
输出:
true
我可以看到你的代码中至少有一个错误。Matcher不会返回任何组,除非您之前调用了
find()
或match()
方法,并且这些方法返回了true
因此,您的代码应该修改如下:
Matcher matcher = pattern.matcher(sourceKey);
return matcher.find() ? oldImageId.equals(matcher.group(3)) : null;
我把它留给你去验证你的正则表达式是否正确 我可以看到您的代码中至少有一个错误。Matcher不会返回任何组,除非您之前调用了
find()
或match()
方法,并且这些方法返回了true
因此,您的代码应该修改如下:
Matcher matcher = pattern.matcher(sourceKey);
return matcher.find() ? oldImageId.equals(matcher.group(3)) : null;
我把它留给你去验证你的正则表达式是否正确 我可以看到您的代码中至少有一个错误。Matcher不会返回任何组,除非您之前调用了
find()
或match()
方法,并且这些方法返回了true
因此,您的代码应该修改如下:
Matcher matcher = pattern.matcher(sourceKey);
return matcher.find() ? oldImageId.equals(matcher.group(3)) : null;
我把它留给你去验证你的正则表达式是否正确 我可以看到您的代码中至少有一个错误。Matcher不会返回任何组,除非您之前调用了
find()
或match()
方法,并且这些方法返回了true
因此,您的代码应该修改如下:
Matcher matcher = pattern.matcher(sourceKey);
return matcher.find() ? oldImageId.equals(matcher.group(3)) : null;
我把它留给你去验证你的正则表达式是否正确 使用regex lookahead和
String#matches()
您的函数可以像
private boolean keyMatches(String sourceKey, String oldImageId){
return sourceKey.matches(".*/(?!.*/)"+oldImageId+"-.*");
}
我尝试了以下测试,将1954
放在URL的各个部分,试图愚弄正则表达式
System.out.println(keyMatches("images/deal/129277/1954-bonus.jpg", "1954"));
System.out.println(keyMatches("images/deal/1954-pics/129277-bonus.jpg", "1954"));
System.out.println(keyMatches("123-1954/1954-00/1954/129277-bonus.jpg", "1954"));
System.out.println(keyMatches("images/deal/129277/129277-1954-bonus.jpg", "1954"));
输出:
使用regex lookahead和
String#matches()
函数可以像
private boolean keyMatches(String sourceKey, String oldImageId){
return sourceKey.matches(".*/(?!.*/)"+oldImageId+"-.*");
}
我尝试了以下测试,将1954
放在URL的各个部分,试图愚弄正则表达式
System.out.println(keyMatches("images/deal/129277/1954-bonus.jpg", "1954"));
System.out.println(keyMatches("images/deal/1954-pics/129277-bonus.jpg", "1954"));
System.out.println(keyMatches("123-1954/1954-00/1954/129277-bonus.jpg", "1954"));
System.out.println(keyMatches("images/deal/129277/129277-1954-bonus.jpg", "1954"));
输出:
使用regex lookahead和
String#matches()
函数可以像
private boolean keyMatches(String sourceKey, String oldImageId){
return sourceKey.matches(".*/(?!.*/)"+oldImageId+"-.*");
}
我尝试了以下测试,将1954
放在URL的各个部分,试图愚弄正则表达式
System.out.println(keyMatches("images/deal/129277/1954-bonus.jpg", "1954"));
System.out.println(keyMatches("images/deal/1954-pics/129277-bonus.jpg", "1954"));
System.out.println(keyMatches("123-1954/1954-00/1954/129277-bonus.jpg", "1954"));
System.out.println(keyMatches("images/deal/129277/129277-1954-bonus.jpg", "1954"));
输出:
使用regex lookahead和
String#matches()
函数可以像
private boolean keyMatches(String sourceKey, String oldImageId){
return sourceKey.matches(".*/(?!.*/)"+oldImageId+"-.*");
}
我尝试了以下测试,将1954
放在URL的各个部分,试图愚弄正则表达式
System.out.println(keyMatches("images/deal/129277/1954-bonus.jpg", "1954"));
System.out.println(keyMatches("images/deal/1954-pics/129277-bonus.jpg", "1954"));
System.out.println(keyMatches("123-1954/1954-00/1954/129277-bonus.jpg", "1954"));
System.out.println(keyMatches("images/deal/129277/129277-1954-bonus.jpg", "1954"));
输出:
您需要使用find函数。您需要使用find函数。您需要使用find函数。如果(m.matches())调用while循环,则最好使用find函数。if(m.matches())为什么要调用
find()
?调用一次就足够了。如果(m.matches())调用while循环为什么要调用find()
?调用一次就足够了。如果(m.matches())调用while循环为什么要调用find()
?调用一次就足够了。如果(m.matches())调用while循环为什么要调用find()
?很高兴再打一次电话。非常感谢。这真的很有帮助。非常感谢。这真的很有帮助。非常感谢。这真的很有帮助。非常感谢。这真的很有帮助。