简体   繁体   English

Java中的pattern和^ pattern $ regEx有什么区别?

[英]What is difference between pattern and ^pattern$ regEx in java?

What is the difference between following two regEx ? 跟着两个regEx有什么区别? both matches exact string in java. 两者都匹配java中的确切字符串。

System.out.println("true".matches("true"));
System.out.println("true".matches("^true$")); // ^ means should start and $ means should end. So it should mean exact match true. right ?

Both prints true . 两者都打印为true

You won't be able to see the difference in your selected string. 您将看不到所选字符串中的差异

Try using: - "afdbtrue" or "tru" with both of them. 尝试使用:-将"afdbtrue""tru"同时使用。 Both strings would not match the first pattern. 这两个字符串都不匹配第一个模式。

^true* -> This means the string should start with t ( Caret(^) means start of string), followed by r and u , and there can be 0 or more e after tru ( u* means 0 or more u) ^true* ->这表示字符串应以t开头( Caret(^)表示字符串的开头),后跟ru ,并且tru后面可以有0或多个eu*表示0或多个u)

System.out.println("tru".matches("^true*"));     // true
System.out.println("trueeeee".matches("^true*"));// true
System.out.println("asdtrue".matches("^true*")); // false

System.out.println("tru".matches("true"));       // false
System.out.println("truee".matches("true"));   // false
System.out.println("asdftrue".matches("true"));  // false
  • Your first and second sysout will print true , because tru starts with t and there is 0 e after tru . 你的first and second系统输出将打印true的,因为tru开头t并有0 etru Same with trueee . trueee相同。 That will be fine 那就好了
  • Your 3rd sysout will print false , because asdtrue does not start with t 您的第3个sysout将输出false ,因为asdtrue不以t开头
  • Your 4th sysout, will again pring false because it is not exactly true 您的第4次sysout将再次false因为它不完全true
  • Your 5th and 6th sysouts will again print false , because they does not exactly matches true 您的5th and 6th sysout将再次输出false ,因为它们与true并不完全匹配

UPDATE : - 更新 :-

After OP changed the question: - OP更改问题后:-

  • ^(caret) matches at the start of the string ^(caret)在字符串开头匹配
  • $(Dollar) matches at the end of the string. $(Dollar)在字符串末尾匹配。

So, ^true$ will match the string with starting with true and ending with true . 因此, ^true$将匹配与开始字符串true ,结束时用true So, now in this case, there won't be any difference between, true and ^true$ in the way you are using. 因此,在这种情况下, true^true$在使用方式上不会有任何区别。

str.matches("true") will match a string that is exactly "true" ., and str.matches("^true$") will also match exactly "true" , because it starts with and ends with "true" . str.matches("true")将匹配完全为"true"的字符串, str.matches("^true$")也将完全匹配"true" ,因为它以"true"开头和结尾。

System.out.println("true".matches("^true$"));     // true
System.out.println("This will not match true".matches("^true$"));   // false
System.out.println("true".matches("true"));       // true
System.out.println("This will also not match true".matches("true")); // false

UPDATE : - 更新 :-

However, if you use Matcher.find method, then there will be a difference in the two pattern. 但是,如果使用Matcher.find方法,则两种模式将有所不同。 Try this: - 尝试这个: -

    Matcher matcher = Pattern.compile("true").matcher("This will be true");
    Matcher matcher1 = Pattern.compile("^true$").matcher("This won't be true"); 

    if (matcher.find()) {  // Will find
        System.out.println(true);
    } else {
        System.out.println(false);
    }
    if (matcher1.find()) {  // Will not find
        System.out.println(true);
    } else {
        System.out.println(false);
    }

OUTPUT : - 输出 :-

true
false

^ represents the start of the string ^代表字符串的开头

$ represents end of the string $代表字符串的结尾

matches method Returns only true if the WHOLE string can be matched matches方法如果可以匹配WHOLE字符串,则仅返回true

So,both "true" & "^true$" would match only a single word ie true 因此, "true""^true$"都只能匹配一个单词,即true


But if you use find method then following would be valid So, "true" would match those lines that contain true anywhere 但是,如果您使用find方法,那么以下内容将是有效的,因此, "true"将匹配那些在任何地方包含true的行

Is it true//match
How true//match
true is truth//match
not false//no match

"^true$" would match any line that that has only one word that is true "^true$"将匹配只有一个为true任何行

true//match
true is truth//no match
it is true//no match

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个正则表达式模式之间有什么区别 - What is the difference between the two regex pattern 口译员模式和访客模式有什么区别? - What is the difference between the Interpreter pattern and Visitor pattern? Java中的工厂设计模式与抽象工厂设计模式有什么区别? - what is the difference between factory and abstract factory design pattern in java? Singleton Design Pattern和Singleton Object在Java中有什么区别? - What is the difference between Singleton Design Pattern and Singleton Object In Java? java中的回调和观察者模式有什么区别 - what's difference between a callback and observer pattern in java Flyweight设计模式和Java缓存之间有什么区别 - What is the difference between Flyweight design pattern and Java cache 此模式的Java正则表达式是什么? - What would be the Java regex for this pattern? Java中Pattern.compile(“ \\\\\\””)和Pattern.compile(“ \\””)的匹配有什么区别? - What is the difference in matching between Pattern.compile(“\\\”“) and Pattern.compile(”\“”) in Java? Front Controller Design Pattern和MVC Design Pattern之间有什么区别 - What is the difference between Front Controller Design Pattern and MVC Design Pattern 工厂设计模式和DAO设计模式有什么区别 - What is difference between factory design pattern and DAO design pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM