0

I am a little bit confused about the ^ symbol in regex.

From what I read online it means : "Finds regex that must match at the beginning of the line."

I read about the example presented here : https://regexone.com/lesson/line_beginning_end

" In the example above, we can use the pattern ^success to match only a line that begins with the word "success", but not the line Error: unsuccessful operation

My confusion comes from the fact that ^success will only match with the string "success" right ? So what is the point of ^ In the examples below ? I would have expect the second to also be true, based on the description of the ^ symbol.

System.out.println(Pattern.matches("^success","success"));  // true
System.out.println(Pattern.matches("^success","success is good"));  // false

Can anyone give me any clear examples with this ^ symbol used in regex?

Teshte
  • 624
  • 1
  • 7
  • 26
  • 1
    `Pattern.matches` tests if regex matches *whole* string, not if some part can be matched by it. That is why `Pattern.matches("^success","success is good")` returns `false` – Pshemo Jan 30 '19 at 09:51
  • ^success anything that starts with and has string success only. so it wont match "success is good" its equal to ^success$ – JineshEP Jan 30 '19 at 09:51
  • @all Then how can I make the check so that the word "success" is first in a string like "success is good" ? Should I have a regular expression that verifies the success part, followed by any number of characters? – Teshte Jan 30 '19 at 09:54
  • @user1653941: Interesting, in many other regex `^success` would not be equal to `^success$` as it would still continue matching. Strange way for `Java` to handle it. – l'L'l Jan 30 '19 at 09:54
  • 3
    @l'L'l it's about the method `matches()`, which checks for a *full match* of the entire string! – bkis Jan 30 '19 at 09:56
  • @mumpitz: Thank you for that, otherwise I was going to say wow... that's insane. – l'L'l Jan 30 '19 at 09:57
  • @l'L'l, if using ^ and or $ , a multiline mode pattern ie "(?m)^success$" will making matching different if using matcher.find(). ie in multiline mode find will match at the start and end of lines. – JineshEP Jan 30 '19 at 11:32

1 Answers1

0

You are right, ^success only matches the string "success".
But ^success still is a pattern to be found in a string like "success is good".

You could check...

System.out.println(Pattern.matches("^success.*","success is good"));

...which should be true, because it matches strings that start with "success" and contain any more characters.
OR you can try to find the pattern ^success in a string. It's a problem of terminology. A pattern matches a string only if it matches the whole string completely. To find a pattern as part of a string (so a substring of it matches the pattern) is a different thing!

A full match of a string also implies ^ and $ (for beginning and end of the string) in the pattern, because the string has to match from beginning to end (thanks to @Pshemo for pointing that out!).

Also see: Using Java to find substring of a bigger string using Regular Expression

bkis
  • 2,530
  • 1
  • 17
  • 31
  • `System.out.println(Pattern.matches("success.*","success is good")); // is also true` ...so your answer made it clearer...but still not 100% why ^ is needed – Teshte Jan 30 '19 at 09:58
  • @Teshte But it doesn't match "my success is good", because "success" is not the beginning, so there's a difference! – bkis Jan 30 '19 at 10:00
  • 3
    @Teshte ^ is not needed for `matches()` since it already requires from regex to match *whole* string (from beginning till its end, so it is like regex is *implicitly* surrounded with `^` and `$`). `^` is useful when you are using `Matcher#find` method, which iterates over *substrings* matching regex. – Pshemo Jan 30 '19 at 10:01
  • @Pshemo exactly, thank you for pointing that out! I will add this to the answer. – bkis Jan 30 '19 at 10:01
  • And now you have 2 problems! `.*` is evil. I learned that the hard way. – YetAnotherBot Jan 30 '19 at 10:09
  • @AdityaGupta True, but it's just an example to clarify that there *can* be more after `^success`. The problems that arise with `.*` are a different story. That's **not** what this question is about. It's about the mechanics of `^` in RegEx to match the beginning of strings! – bkis Jan 30 '19 at 10:15
  • Makes sense. It's just that users copy a working code and leave it as is in the code. Even static code analyzers don't pick these things. – YetAnotherBot Jan 30 '19 at 10:19
  • @AdityaGupta Well, as bad as `.*` can be, the OP asked how to actually use `^` to match a string that starts with a certain pattern, whatever comes after that. In Java, the given RegEx does just that. – bkis Jan 30 '19 at 10:22