I checked the documentation, and cannot find what [\w-]
means. Can anyone tell me what [\w-]
means in Ruby?
\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.
The regular expression \s is a predefined character class. It indicates a single whitespace character. Let's review the set of whitespace characters: [ \t\n\x0B\f\r] The plus sign + is a greedy quantifier, which means one or more times. For example, expression X+ matches one or more X characters.
In a regular expression, if you have [a-z] then it matches any lowercase letter. [0-9] matches any digit. So if you have [a-z0-9], then it matches any lowercase letter or digit.
?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).
The square brackets []
denote a character class. A character class will match any of the things inside it.
\w
is a special class called "word characters". It is shorthand for [a-zA-Z0-9_]
, so it will match:
a-z
(all lowercase letters)A-Z
(all uppercase letters)0-9
(all digits)_
(an underscore)The class you are asking about, [\w-]
, is a class consisting of \w
and -
. So it will match the above list, plus hyphens (-
).
Exactly as written, [\w-]
, this regex would match a single character, as long as it's in the above list, or is a dash.
If you were to add a quantifier to the end, e.g. [\w-]*
or [\w-]+
, then it would match any of these strings:
fooBar9
foo-Bar9
foo-Bar-9
-foo-Bar---9abc__34ab12d
And it would partially match these:
foo,Bar9 # match 'foo' - the ',' stops the match
-foo-Bar---9*bc__34ab12d # match '-foo-Bar---9', the '*' stops the match
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