Reputation: 2772
Can some body explain the difference between below expressions please?
[0-9]{1,3}:[0-5][0-9]
^([0-9]{1,3}:[0-5][0-9])$
I don't get the exact usage of ^ sign and $ sign in regular expressions.
I observe that If I write the second expression as below, it didn't make any difference.
^([0-9]{1,3}):([0-5][0-9])$
Upvotes: 3
Views: 282
Reputation: 195039
examples explain it clear:
^ : matches the beginning of a line
$ : matches the end of a line
"^foo$" : matches "foo", but not " foo" or "xxfooyy"
"foo$" : matches "foo", " foo" or "xxfoo" but not "foobar"
"^foo" : matches "foo", "fooyy" or "foo " but not "xfoo"
"foo" : matches "foo", " foo" or "xxfooyy"
Upvotes: 4
Reputation: 24551
^
= start of string/line
$
= end of string/line
So your first expression will also match "FOO123:12BAR"
The difference between your second and third expression is not in what they match but what they capture, as the parantheses for capture groups differ.
Upvotes: 2
Reputation: 382132
^
and $
are here the start and end of string anchors.
The second regular expression means you want to match the whole input.
Upvotes: 2