473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Ruby regex for removing C/Java-style /* ... */ comments

Hey guys,

As the title says I'm trying to make a regular expression (regex/regexp) for use in removing the comments from code. In this case, this particular regex is meant to match /* ... */ comments.

I'm using Ruby v.1.8.6

Here's my regex:
Expand|Select|Wrap|Line Numbers
  1. multiline_comments = /\/\*(.*?)\*\//
When I try
Expand|Select|Wrap|Line Numbers
  1. myStr.gsub(multiline_comments, "")
I see no effect. The string has big fat comments in it too. I tried using this regex in irb with a couple test strings, and it works perfectly. This leads me to think I don't understand some subtlety of file io, so here's all my code (cop-out, I know). I'm trying to write a very simple JavaScript compacter, but I want to preserve readability so I'm only getting rid of unnecessary newlines, spaces in between tokens, and comments. I DON'T want the whole file on one line like some compactors do it. Anyway, here goes:

Expand|Select|Wrap|Line Numbers
  1. # Non-destructive JavaScript Packer
  2. # =================================
  3. #
  4. # Reduces overall script filesize by removing comments
  5. # and unecessary whitespace. Does not affect variable naming,
  6. # indentation, or line-by-line formatting in order to maximize
  7. # readability.
  8.  
  9. def pack_line(file_line)
  10.  
  11.     return '' unless file_line
  12.  
  13.     #puts "The next line: " + file_line
  14.  
  15.     #kill one-line (//...) comments
  16.     line_comments = /(\S*)\s*\/\/.*/
  17.     intermed = file_line.gsub(line_comments, '\1')
  18.     intermed += "\n" if intermed[intermed.length - 1] != "\n"
  19.  
  20.     #puts "\tAfter one-liner removal: " + intermed
  21.  
  22.     #kill unnecessary whitespace
  23.     extra_whitespace = /([^(var|function|return|\s*)])[ \t]+(.*?)/
  24.     intermed = intermed.gsub(extra_whitespace, '\1\2')
  25.  
  26.     #puts "\tAfter extra whitespace removal: " + intermed
  27.  
  28.     intermed
  29. end
  30.  
  31. #performs the packing operation, returns a single string
  32. #representing the packed document
  33. def pack(file)
  34.     lines = Array.new
  35.  
  36.     file.each_line do |line|
  37.         lines.push pack_line(line)
  38.     end
  39.  
  40.     intermed = lines.join
  41.  
  42.     #puts "\tBefore multi-liner removal: " + intermed
  43.  
  44.     #kill multi-line (/* ... */) comments
  45.     multiline_comments = /\/\*(.*?)\*\//
  46.     intermed = intermed.gsub(multiline_comments, '')
  47.  
  48.     #puts "\tAfter multi-liner removal: " + intermed
  49.  
  50.     #kill extra new lines
  51.     extra_newlines = /(\r?\n){2,}/
  52.     intermed = intermed.gsub(extra_newlines, "\n")
  53.  
  54.     #puts "\tFinally: " + intermed + "\n"
  55.  
  56.     intermed
  57. end
  58.  
  59. #open file for reading and pass it to pack()
  60. def init(in_file, out_file)
  61.     file = File.new(in_file, "r")
  62.  
  63.     newDoc = pack(file)
  64.  
  65.     file.close
  66.  
  67.     if out_file then
  68.         file = File.new(out_file, "w")
  69.  
  70.         file.puts(newDoc)
  71.  
  72.         file.close
  73.     else
  74.         puts newDoc
  75.     end
  76. end
  77.  
  78. #start the script with the command-line arg file name
  79. puts init(ARGV[0], ARGV[1])
  80.  
Any ideas? Thanks for all your help.
Jul 31 '07 #1
2 7305
More specifically, even though it works fine on one-line strings, I think I've found that it's unable to match this style of comments across new lines ("\n"). Is there a way to get around this? I thought the '.' matched any character whatsoever...
Aug 1 '07 #2
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/11137
Aug 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
65
by: Nikolas Hagelstein | last post by:
Hi, First of all: i spend a lot of time on thinking and researching about how to make developing webapplications with php a more structured/systematic thing. I even fancied to switch over to...
65
by: Amol Vaidya | last post by:
Hi. I am interested in learning a new programming language, and have been debating whether to learn Ruby or Python. How do these compare and contrast with one another, and what advantages does one...
16
by: Andrew Baker | last post by:
I am trying to write a function which provides my users with a file filter. The filter used to work just using the VB "Like" comparision, but I can't find the equivilant in C#. I looked at...
122
by: seberino | last post by:
I'm interested in knowing which Python web framework is most like Ruby on Rails. I've heard of Subway and Django. Are there other Rails clones in Python land I don't know about? Which one...
44
by: John A. Bailo | last post by:
Dr. Dobbs has a /glowing/ article on Ruby on Rails this month. What do you guys think? Can it replace .net, php and java? And be the Open Source OOP web solution that is not bound to Sun or...
9
by: Erwin Moller | last post by:
Hi Group, This may seem a odd question in a PHP group, but I think this might be a good place to ask since I am mainly a PHP coder these days that maybe starts with Ruby. Situation: A client...
0
by: vmysore | last post by:
I am trying to get all the columns selected within a SQL query (including the sub selects). When the code hits matcher.find(). i get the following exception: Exception in thread "main"...
4
by: sukatoa | last post by:
This was my first time to encouter this kind of exception.... that exception appears when i invoked the the method below. private final String encrypting(String enc){ int...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.