0

I want to learn the working of ^ and $ symbols in Java Regular Expression.

$ for end. Ok fine.

1: What is the meaning of ^ symbol?

Somewhere I read ^ used to start the string, but I read from the Oracle Doc that ^ used for abstraction as [^abc] means anything except abc.

2: How ^ and $ works.

when I write $ at the end then it gives me the ending match. 71234567897123456780

Pattern p = Pattern.compile("[789][0-9]{9}$");
Matcher m = p.matcher("71234567897123456780");
while(m.find()){
    System.out.println(m.group());
}

Output: 7123456780

when I write ^ at the start then it gives me the starting match. 71234567897123456780

Pattern p = Pattern.compile("[789][0-9]{9}$");
Matcher m = p.matcher("71234567897123456780");
while(m.find()){
    System.out.println(m.group());
}

Output: 7123456789

and when I use ^ and $ same time then it gives me no output as I read from here that it reads complete line that's why no match because number increases than 10.

I know the meaning of [789], [0-9], {9} and also know that the string matches two time. first time 7123456789 and second time 7123456780. But I want to learn the working of ^ and $ symbol.

There is similar question with less information than I required. ^ and $ in Java regular expression

Community
  • 1
  • 1
  • 1. Those are both true. – SLaks Nov 15 '16 at 19:34
  • @SLaks when meaning changes? mean where `^` will work as abstraction or starting of string? –  Nov 15 '16 at 19:37
  • 2
    The caret (`^`) is interpreted differently depending if it's enclosed in square brackets (`[` and `]`). If they are, then it denotes an exclusion (anything except a, b or c). If not, then it denotes the beginning of the string. – Chris Forrence Nov 15 '16 at 19:37
  • @Nothing - `^` only means "not" when it's inside square brackets. – Dawood ibn Kareem Nov 15 '16 at 19:38
  • @ChrisForrence what's about the remaining part of question? –  Nov 15 '16 at 19:39
  • Note that `$` is also interpreted differently if it is inside square brackets: then it's a literal `$`. – Andy Turner Nov 15 '16 at 19:43
  • @Nothing, are you asking about what happens when both are used in the expression (`^[789][0-9]{19}$`)? – Chris Forrence Nov 15 '16 at 19:45
  • when you use `^` and `$` at same time, it will give **no match** because your 10th character is not `end of string` (or `$`). In this case, it would only match if your subject string is, for eg. `712345678` ([see here](https://regex101.com/r/sS5vgP/1)) – Caio Oliveira Nov 15 '16 at 19:45
  • @ChrisForrence yes both and separately too. –  Nov 15 '16 at 19:47
  • I mean, in the question, you seem to already understand what they mean. `^` anchors the regular expression to the start of the line, `$` anchors it to the end of the line, so using both anchors the expression to encompass the entire string. – Chris Forrence Nov 15 '16 at 19:49
  • @ChrisForrence I mean when I add `^` then why I get just starting match, why not 2 time? `7123456789` and `7123456780` and when I use `$` then why it just print the ending match? does `^` mean start the string and end once the first match found? and `$` mean get the last match? –  Nov 15 '16 at 19:55

0 Answers0