I want to create regular expression to match ruby symbols, but I need to know what the exact syntax for a symbol is.
Until now I am aware of the following:
:'string'
:"string"
:__underline
:method
:exclamation!
:question?
:@instance
:$global
It's not entirely clear what you are talking about.
If you are talking about what a Symbol
can contain, the answer is: anything and everything, including newlines, arbitrary whitespace, control characters, arbitrarily weird and obscure Unicode characters, and everything else.
If you are talking about the various ways of writing Symbol
literals, here's my best understanding:
:
literal: any valid Ruby identifier (e.g. :foo
, :Foo
, :@foo
, :@@foo
, :$foo
, :$:
, …):
literal: everything that's valid in a single-quoted String
literal, including escape sequences such as :'\''
and :'\\'
:
literal: everything that's valid in a double-quoted String
literal, including escape sequences such as :"\""
, :"\\"
, and :"\n"
, as well as string interpolation, which allows you to inject the results of arbitrary Ruby code into the Symbol
, e.g. :"#{if rand < 0.5 then RUBY_VERSION else ENV['HOME'] end}"
Array
of Symbol
s literal: everything that's valid in a single-quoted Array
of String
s literal, e.g. %i|foo bar baz|
(equivalent to [:foo, :bar, :baz]
), %i(foo\ bar baz)
(equivalent to [:'foo bar', :baz]
), %i:foo bar:
(equivalent to [:foo, :bar]
)Array
of Symbol
s literal: everything that's valid in a double-quoted Array
of String
s literal, e.g. %I|foo #{bar} baz|
, etc.Symbol
hash keys in the key: value
syntax: every valid Ruby label, e.g. {foo: 42}
Symbol
hash keys in the quoted 'key': value
syntax: every valid Ruby String
literal, including escape sequences and interpolation, e.g. {"foo\n#{bar}": 42}
There are of course a lot of other expressions that evaluate to Symbol
s:
def foo;end # => :foo
String#to_sym
(alias String#intern
): 'foo bar'.to_sym # => :'foo bar'
return
a Symbol
http://www.cse.buffalo.edu/~regan/cse305/RubyBNF.pdf enumerates the context-free grammar productions that define Ruby's syntax. CFGs are inherently more powerful than REs, so you might want to consider a different tool for this job--but you can certainly look at this document and try to construct a regexp that matches all cases.
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