473,562 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

16 New Member
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.
  9. def pack_line(file_line)
  11.     return '' unless file_line
  13.     #puts "The next line: " + file_line
  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"
  20.     #puts "\tAfter one-liner removal: " + intermed
  22.     #kill unnecessary whitespace
  23.     extra_whitespace = /([^(var|function|return|\s*)])[ \t]+(.*?)/
  24.     intermed = intermed.gsub(extra_whitespace, '\1\2')
  26.     #puts "\tAfter extra whitespace removal: " + intermed
  28.     intermed
  29. end
  31. #performs the packing operation, returns a single string
  32. #representing the packed document
  33. def pack(file)
  34.     lines = Array.new
  36.     file.each_line do |line|
  37.         lines.push pack_line(line)
  38.     end
  40.     intermed = lines.join
  42.     #puts "\tBefore multi-liner removal: " + intermed
  44.     #kill multi-line (/* ... */) comments
  45.     multiline_comments = /\/\*(.*?)\*\//
  46.     intermed = intermed.gsub(multiline_comments, '')
  48.     #puts "\tAfter multi-liner removal: " + intermed
  50.     #kill extra new lines
  51.     extra_newlines = /(\r?\n){2,}/
  52.     intermed = intermed.gsub(extra_newlines, "\n")
  54.     #puts "\tFinally: " + intermed + "\n"
  56.     intermed
  57. end
  59. #open file for reading and pass it to pack()
  60. def init(in_file, out_file)
  61.     file = File.new(in_file, "r")
  63.     newDoc = pack(file)
  65.     file.close
  67.     if out_file then
  68.         file = File.new(out_file, "w")
  70.         file.puts(newDoc)
  72.         file.close
  73.     else
  74.         puts newDoc
  75.     end
  76. end
  78. #start the script with the command-line arg file name
  79. puts init(ARGV[0], ARGV[1])
Any ideas? Thanks for all your help.
Jul 31 '07 #1
2 7324
16 New Member
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
1 New Member
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

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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it...
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 FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature...
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 jsp since i heard all those stiltedness rumors about it's professionalism/stability etc. Anyway my current attitude is that fullfilling the...
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 language provide over the other? I would like to consider as many opinions as I can on this matter before I start studying either language in depth....
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 RegEx.IsMatch but it behaves quite differently. Is there a way I can mimic the DOS filtering of filenames (eg. "*.*" or "*" returns all files, "*.xls"...
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 has largest community/buzz about it?
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 MS? http://media.rubyonrails.org/presentations/pursuitofbeauty.pdf
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 of a friend of mine asked me to take over a project done in Ruby. (The original programmer appearantly behaved like an @ss and his client wants to...
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" java.lang.StackOverflowError at java.util.regex.Pattern$Branch.match(Pattern.java:4530) at java.util.regex.Pattern$GroupHead.match(Pattern.java:4570) I am...
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 declen=array1.length; String temp=enc; for(int x=-1;x++<declen;){ temp=temp.replaceAll(String.valueOf(array2),String .valueOf(array1));
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.