Is there any difference between (\w+)?
and (\w*)
in regex?
It seems the same, doesn't it?
Quick answer: ^[\w*]$ will match a string consisting of a single character, where that character is alphanumeric (letters, numbers) an underscore ( _ ) or an asterisk ( * ). Details: The " \w " means "any word character" which usually means alphanumeric (letters, numbers, regardless of case) plus underscore (_)
\w (word character) matches any single letter, number or underscore (same as [a-zA-Z0-9_] ). The uppercase counterpart \W (non-word-character) matches any single character that doesn't match by \w (same as [^a-zA-Z0-9_] ). In regex, the uppercase metacharacter is always the inverse of the lowercase counterpart.
represents a single character (like the regex's . ) while * represents a sequence of zero or more characters (equivalent to regex . * ).
*? is non-greedy. * will match nothing, but then will try to match extra characters until it matches 1 , eventually matching 101 . All quantifiers have a non-greedy mode: . *? , .
(\w+)?
and (\w*)
both match the same (0..+inf word characters)
However, there is a slight difference:
In the first case, if this part of the regex matches ""
, the capturing group is absent. In the second case, it is empty. In some languages, the former manifests as a null
while the latter should always be ""
.
In Javascript, for example,
/(\w*)/.exec("") // ["", ""]
/(\w+)?/.exec("") // ["", undefined]
In PHP (preg_match
), in the former case, the corresponding key is simply absent in the matches array: http://3v4l.org/DB6p3#v430
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With