Rule for a regex enclosed by two /

95 views Asked by At

How can I match everything between a pair of / characters with treetop? I would also like to match escaped / characters as well. For example, if I were to parse a "regex":

/blarg: dup\/md5 [0-9a-zA-Z]{32}/

The result would return:

blarg: dup\/md5 [0-9a-zA-Z]{32}
1

There are 1 answers

1
Josh Voigts On BEST ANSWER

This should match everything inside two / characters including escaped slashes. I'm using Ruby's DATA __END__ feature so that everything can run in a single file.

Also, note that you can tag parts of a parsed expression and then use them as functions. In the example below I tagged inside. This could also have been accessed as elements[1] instead of being tagged.

This works similar to matching a string which you can find in the treetop docs.

require 'treetop'
Treetop.load_from_string DATA.read

parser = RegexParser.new

puts parser.parse('/blarg: dup\/md5 [0-9a-zA-Z]{32}/').inside.text_value

# => blarg: dup\/md5 [0-9a-zA-Z]{32}

__END__
grammar Regex
   rule regex
      '/' inside:('\/' / !'/' .)* '/'
   end
end