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