I’m trying to understand the following code:
Pattern.compile("(.*?):")
I already did some research about what it could mean,
but I don’t quite get it:
According to the java docs the * would mean 0 or more times,
while ? means once or not at all.
Also, what does the ‘:’ mean?
Thanks
The
?after greedy operators such as+or*will make the operator non greedy. Without the?, that regex will keep matching all the characters it finds, including the:.As it is, the regex will match any string which happens before the semi colon (
:). In this case, the semicolon is not a special character. What ever comes before the semicolon, will be thrown into a group, which can be accessed later through aMatcherobject.This code snippet will hopefully make things more clear:
Yields: