Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ([^:"]+) do in a Ruby regular expression?

Tags:

regex

ruby

I can't find a good reference for the apparently special uses of the ^, :, and " characters.

like image 640
Chris Avatar asked Jan 17 '11 06:01

Chris


People also ask

What does =~ mean in Ruby regex?

=~ 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.

What does () mean in regular expression?

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.

What does (? I do in regex?

(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.


2 Answers

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 more

See also: Character Classes

like image 88
Kobi Avatar answered Oct 16 '22 06:10

Kobi


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

like image 25
Gumbo Avatar answered Oct 16 '22 04:10

Gumbo