I can't find a good reference for the apparently special uses of the ^
, :
, and "
characters.
=~ is Ruby's basic pattern-matching operator. When one operand is a regular expression and the other is a string then the regular expression is used as a pattern to match against the string. (This operator is equivalently defined by Regexp and String so the order of String and Regexp do not matter.
The () will allow you to read exactly which characters were matched. Parenthesis are also useful for OR'ing two expressions with the bar | character. For example, (a-z|0-9) will match one character -- any of the lowercase alpha or digit.
(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.
It matches a block of characters that are not :
or "
.
[...]
- Character classes - match characters in this class. For example, [abc]
, would match one character, a
or b
or c
.[^...]
- Negated Character class.+
- match one or moreSee also: Character Classes
The syntax […]
is a character class that matches one of the character as described inside. With [^…]
the character class is inverted to that it matches any character except the ones as described inside.
So [^:"]
describes any arbitrary character except :
and "
. And ([^:"]+)
is a group that matches one or more arbitrary characters except :
and "
.
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