Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex difference: (\w+)? and (\w*)

Is there any difference between (\w+)? and (\w*) in regex?

It seems the same, doesn't it?

like image 477
爱国者 Avatar asked Jan 17 '13 06:01

爱国者


People also ask

What does W * mean regex?

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 (_)

What does W and W mean in regex?

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

What is the difference between * and * in regex?

represents a single character (like the regex's . ) while * represents a sequence of zero or more characters (equivalent to regex . * ).

What is the difference between * and *??

*? 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: . *? , .


1 Answers

(\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

like image 119
John Dvorak Avatar answered Sep 26 '22 21:09

John Dvorak