A star is very similar to a plus, the only difference is that while the plus matches 1 or more of the preceeding character/group, the start matches 0 or more.
I think the previous answers fail to highlight a simple example:
for example we have an array:
numbers = [5, 15]
The following regex expression ^[0-9]+ matches: 15 only.
However, ^[0-9]* matches both 5 and 15. The difference is that the + operator requires at least one duplicate of the preceding regex expression
A
+
matches one or more instances of the preceding pattern. A*
matches zero or more instances of the preceding pattern.So basically, if you use a
+
there must be at least one instance of the pattern, if you use*
it will still match if there are no instances of it.+
matches at least one character*
matches any number (including 0) of charactersThe
?
indicates a lazy expression, so it will match as few characters as possible.A star is very similar to a plus, the only difference is that while the plus matches 1 or more of the preceeding character/group, the start matches 0 or more.
Consider below is the string to match.
The pattern
(ab.*)
will return a match for capture group with result ofab
While the pattern
(ab.+)
will not match and not returning anything.But if you change the string to following, it will return
aba
for pattern(ab.+)
+
is minimal one,*
can be zero as well.I think the previous answers fail to highlight a simple example:
for example we have an array:
The following regex expression
^[0-9]+
matches:15
only. However,^[0-9]*
matches both5 and 15
. The difference is that the+
operator requires at least one duplicate of the preceding regex expression