Ruby:
- Mainly worked on better understanding the processFile.rb code and regular expressions
- Learned about the MatchData object in ruby
- In [code 1], the matches variable is a MatchData object. It stores the full matched string as well as the individual values of the things within the parentheses of the regex, which are called backtraces according to the ruby docs
- [Code 2] illustrates how this works. The parentheses match all text within the xml tags, which is stored in md[1]. The full string matched is stored in md[2]
- Learned about the m option of regex, which makes . (all characters) match new lines (\n) as well
- For example, /.*/m would match all characters, including newlines.
Code:
processxref1 = '<?cross-reference-to-related-applications ... ?>'
processxref2 = '<?cross-reference-to-related-applications ... ?>' matches = app.to_s.match(/#{Regexp.escape(processxref1)}(.*)#{Regexp.escape(processxref2)}/m)
if matches Nokogiri::XML.fragment(matches[1].strip).xpath("./p/text()").to_s.gsub(/\n/, "")
text = "<abc>text</abc>"
md = text.match %r{<abc>(.*)</abc>}m
puts md[0] #=> <abc>text</abc>
puts md[1] #=> text
No comments:
Post a Comment