views:

260

answers:

7

Can someone explain what the following regexp matches?

^.*$

Thank you!

+20  A: 

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

LukeH
A: 

It looks like it matches everything...

Dismissile
+14  A: 

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).

dave
Thank you for the explanation
user73829
+1 for actually explaining it
Michael Mrozek
+1 for a good simple explanation
By default, most regex implementations will not match `\r` and `\n` for the DOT meta char. So, not "everything".
Bart Kiers
@Bart K. Thanks I missed that.
dave
+1  A: 

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.

unholysampler
+2  A: 

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 
bunting
+1  A: 

^ = 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).

Jason Webb
A: 

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).

krs1