Priyank Doshi
Priyank Doshi

Reputation: 13151

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

What is the difference between following two regEx ? both matches exact string in 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.

Upvotes: 1

Views: 1307

Answers (2)

Rohit Jain
Rohit Jain

Reputation: 213223

You won't be able to see the difference in your selected string.

Try using: - "afdbtrue" or "tru" with both of them. 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)

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. Same with trueee. That will be fine
  • Your 3rd sysout will print false, because asdtrue does not start with t
  • Your 4th sysout, will again pring false because it is not exactly true
  • Your 5th and 6th sysouts will again print false, because they does not exactly matches true

UPDATE: -

After OP changed the question: -

  • ^(caret) matches at the start of the string
  • $(Dollar) matches at the end of the string.

So, ^true$ will match the string with starting with true and ending with true. So, now in this case, there won't be any difference between, true and ^true$ in the way you are using.

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".

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. 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

Upvotes: 1

Anirudha
Anirudha

Reputation: 32797

^ represents the start of the string

$ represents end of the string

matches method Returns only true if the WHOLE string can be matched

So,both "true" & "^true$" would match only a single word i.e true


But if you use find method then following would be valid So, "true" would match those lines that contain true anywhere

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//match
true is truth//no match
it is true//no match

Upvotes: 0

Related Questions