re = /(?:\$\w+(?:\((?<arg>(?R)|(?:\"[^\"]*\")|(?:[^\$\"\&\s\(\)][^\s\(\)]*))(?:(?:\+| )(?&arg))*\))?)|(?<var>\&\w+)/m
str = '$test($test(test &var1 $var2) $test(jean) jean) # should full-match
$test($test(test $var1 $var2) $test(foo) bar) # should not full-match
$test("&test") # should not full-match
$test("foo&test") # should not full-match
$test("&test" &test) # should full-match
&test # should full-match
$test(" &test") # should not full-match
$test # should not full-match
&test(foo) # should not full-match
&test(&foo $bar()) # should not full-match
$test((&test)) # should not full-match
$foo(bar(&baz)) # should not full match
$test(&test &test) # should not full match
$test( &test) # should not full match
$test(&test ) # should not full match'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html