-3

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}
Josh Voigts
  • 4,114
  • 1
  • 18
  • 43
Jym Morrison
  • 185
  • 1
  • 3
  • 9
  • I think you want to get out the string from the regex. If so use `/blarg: dup\/md5 [0-9a-zA-Z]{32}/.source # => "blarg: dup/md5 [0-9a-zA-Z]{32}"` .. I am not clear about the question, so I put it in the comment itself. – Arup Rakshit Jun 01 '14 at 15:00
  • possible duplicate of [Treetop basic parsing and regular expression usage](http://stackoverflow.com/questions/2404518/treetop-basic-parsing-and-regular-expression-usage) – John Dvorak Jun 01 '14 at 15:04
  • What do you mean by "get"? You want to refer to it in subsequent operations? In that case, enclose the whole thing in ( .. ) and then you can refer to it with \1 – Mr Lister Jun 01 '14 at 15:07
  • 1
    Just want a regex compatible with the treetop rule format that matches /blarg: dup\/md5 [0-9a-zA-Z]{32}/ – Jym Morrison Jun 01 '14 at 16:03
  • This is a valid question and should not be getting down votes. – Josh Voigts Jun 02 '14 at 21:09

1 Answers1

0

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
Josh Voigts
  • 4,114
  • 1
  • 18
  • 43