I know I can do this with a regex, but...

T

Tman

Need to strip comments from VBScript code.

Tried to use a state machine to tell if I was in quotes, but got very
confused. I know I can use a regex, although I may have to call it
repeatedly to eat quoted strings. Anyone have ideas?

I need to whack everything after the first comment character...

this is not a comment
'this line is fully commented
the comment 'in this line starts after ' the word comment
there "is no 'comment" in this line
but in "this 'line" there 'is a "comment" before the word "is"
'this line is "fully" commented

ad infinitum...
 
A

Anno Siegel

Tman said:
Need to strip comments from VBScript code.

Tried to use a state machine to tell if I was in quotes, but got very
confused. I know I can use a regex, although I may have to call it
repeatedly to eat quoted strings. Anyone have ideas?

I need to whack everything after the first comment character...

this is not a comment
'this line is fully commented
the comment 'in this line starts after ' the word comment
there "is no 'comment" in this line
but in "this 'line" there 'is a "comment" before the word "is"
'this line is "fully" commented

Take a look at Regex::Common. It has elaborate regexes that deal
with comments.

Anno
 
P

Philip Lees

Need to strip comments from VBScript code.

Tried to use a state machine to tell if I was in quotes, but got very
confused. I know I can use a regex, although I may have to call it
repeatedly to eat quoted strings. Anyone have ideas?

I need to whack everything after the first comment character...

The Perl FAQ gives a regex for doing this kind of splitting. However,
as you're only concerned about breaking into two pieces, it might be
easier to do it this way (if you don't care about efficiency).

while (<DATA>){
chomp;
my $line = "";
foreach( split // ){
unless ( /"/ ... /"/ ){ last if /'/ }
$line .= $_
}
print "$line\n" if $line;
}

__DATA__
this is not a comment
'this line is fully commented
the comment 'in this line starts after ' the word comment
there "is no 'comment" in this line
but in "this 'line" there 'is a "comment" before the word "is"
'this line is "fully" commented

Phil
 
B

bd

Tman said:
Need to strip comments from VBScript code.

Tried to use a state machine to tell if I was in quotes, but got very
confused. I know I can use a regex, although I may have to call it
repeatedly to eat quoted strings. Anyone have ideas?

I need to whack everything after the first comment character...

this is not a comment
'this line is fully commented
the comment 'in this line starts after ' the word comment
there "is no 'comment" in this line
but in "this 'line" there 'is a "comment" before the word "is"
'this line is "fully" commented

ad infinitum...

How about:
sub stripcomment ($) {
my $str = shift @_;
my @chars = split '', $str;
my @out;
my $inquote = 0;
for(@chars){
if($_ eq "'" && !$inquote){
last;
}
if($_ eq '"'){
$inquote = !$inquote;
}
push @out, $_;
}
return join '', @out;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top