user73829
user73829

Reputation: 41

What does the following ^.*$ regexp match?

Can someone explain what the following regexp matches?

^.*$

Thank you!

Upvotes: 3

Views: 466

Answers (7)

krs1
krs1

Reputation: 1135

It looks like it matches everything including empty strings. The .* means that it matches everything (the period) 0 or more times (the *). The ^ and the $ are redundant if you have set the multline flag (not sure what it is in java).

Upvotes: 0

Jason Webb
Jason Webb

Reputation: 8020

^ = Start of string or line (depends on settings).

. = Any character.

* = Any number of the previous character. In this case the ..

$ = End of string or line (depends on settings).

Put them together and it can match either a whole string or one whole line depending on what the multiline settings are (see this for more info).

Upvotes: 1

Kai Sternad
Kai Sternad

Reputation: 22830

It matches all empty and non-empty lines. It can be broken down into the following parts:

^ : match the beginning of the line
. : match any character except newline
* : match zero or many instances of the match
$ : match the ending of the line 

Upvotes: 2

unholysampler
unholysampler

Reputation: 17321

It will match anything.

^ signifies the start of the line. $ signifies the end of the line. So this means that the expression must match the entire string it is passed.

. will match any single character. * means that the thing before it can appear between 0 to any number of times. So this means that the string can have any number of characters, including 0.

Upvotes: 1

dave
dave

Reputation: 12806

everything.

^ is the beginning of the string. 
. is any character. 
* means 0 or more of said characters. 
$ is the end of the string. 

So this regex matches 0 or more characters that start and end a string (which is everything).

Upvotes: 15

Dismissile
Dismissile

Reputation: 33071

It looks like it matches everything...

Upvotes: 0

LukeH
LukeH

Reputation: 269348

Either the entire string or the entire line, depending on whether multiline mode is used.

Upvotes: 21

Related Questions