Kitz Forum

Chat => Tech Chat => Topic started by: Weaver on August 09, 2021, 04:35:44 AM

Title: Regex question (again)
Post by: Weaver on August 09, 2021, 04:35:44 AM
I have tried a number of regexes over the years to solve the problem of skipping over C-style /* */ comments, or stripping such comments out. For simplicity’s sake I’m neglecting all issues of backslash literals. What’s your favourite regex for this?
Title: Re: Regex question (again)
Post by: chenks on August 09, 2021, 08:01:57 AM
did you search stackoverflow for anything?
first search hit is https://stackoverflow.com/questions/51312345/regex-to-find-a-string-excluding-comments
Title: Re: Regex question (again)
Post by: burakkucat on August 09, 2021, 04:52:38 PM
I have tried a number of regexes over the years to solve the problem of skipping over C-style /* */ comments, or stripping such comments out. For simplicity’s sake I’m neglecting all issues of backslash literals. What’s your favourite regex for this?

A few years ago, I very nearly started to write a utility to do a similar task . . . but then I had a sudden thought "Why bother? The C-preprocessor will do it for me". For my then task that was sufficient.  ;)
Title: Re: Regex question (again)
Post by: Weaver on August 09, 2021, 07:18:06 PM
I did search stackoverflow

I have successfully arrived at a simplified regex,  as I said earlier, for a C-style grammar without the backslash literals of C, but I wanted to hear your alternative suggestions, to see if they are more elegant than my effort.

One of the recent successful attempts is as follows:

    \/\*\X*?\*\/


I seem to remember I did an alternative regex using lookahead, but I can’t find it just now.

There’s also the question of which regex engine you use, as engines differ in their capabilities. I use https://regex101.com/ for testing and have  tried out the various choices for emulated engines that the website’s own sophisticated engine offers. I am using the regex within Apple’s iOS Shortcuts’ regex ‘match’ and ‘replace’ functions.