From: Kero van Gelder Date: 2001-07-17T19:46:00+09:00 Subject: [ruby-talk:17983] Re: Regexp - stripping comments > I'm trying to get the beginning of a line before a comment. A comment is a > semicolon. The problem I'm having is that the semicolons may be in "" which > I want to keep. > > For example: > > word1 word2 "some text possibly with ;" + __FILE__ + "some more text" ; > comments Your familiar with the concept of "string" =~ /regexp/ ? If so, you probably came up with something like if 'if expr ;see' =~ /([^;]*);(.*)/ p $1, $2 end printing the two substrings matching the parts between braces. But the problem lies in the quoted strings: if 'if "blah; blah" ;see' =~ /([^;"]*"[^"]*"[^;"]*);(.*)/ p $1, $2 end will work on this line, but not if there are more "quoted" "strings", so if 'if "blah;" and "bah;" ;see' =~ /([^;"]*("[^"]*"[^;"]*)*);(.*)/ p $1, $2, $3 end and $1 still holds the beginning of the line. Bye, Kero. PS: I am not a regexp guru, so I imagine shorter, clearer or more efficient solutions exist. Plus, next thing he'll ask is how to escaped double quotes can be allowed in the quoted string (like 'if "bla(\"x;y\")"' ) and I know of no easy extension of my example to achieve that :( PS2: Won't match if there is no semicolon on the line, but you can figure that one out yourself now (hint: ?) +--- Kero ------------------------------ kero@chello.nl ---+ | Don't split your mentality without thinking twice | | Proud like a God -- Guano Apes | +--- M38c ------- http://members.chello.nl/~k.vangelder ---+