lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> I have following problem with strings. I want to use string.gsub to 
> modify big string. However I don't want string.gsub to affect whole 
> string. There are some parts that should be left as they are. For a 
> example in a string containing C language file you could let string.gsub 
> modify everything else except chars between the comments (/* .. */). Any 
> ideas how to implement this?

The only fool-proof way to handle comments in C is to perform lexical analysis,
because /* ... */ might appear inside a string. This cannot be done by pattern
matching.

But try this trick:

1. Replace /* ... */ by @xxx where xxx is a number that is different for
   each comment. Choose a syntax for @xxx that does not conflict with
   the editing your need to do. Remember the contents of the comments for 3.

2. Do your editing on the modified string.

3. Restore the comments.

All this is easily done with gsub.
--lhf