Kitz ADSL Broadband Information
adsl spacer  
Support this site
Home Broadband ISPs Tech Routers Wiki Forum
 
     
   Compare ISP   Rate your ISP
   Glossary   Glossary
 
Please login or register.

Login with username, password and session length
Advanced search  

News:

Author Topic: Regex question (again)  (Read 1853 times)

Weaver

  • Senior Kitizen
  • ******
  • Posts: 11459
  • Retd s/w dev; A&A; 4x7km ADSL2 lines; Firebrick
Regex question (again)
« 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?
Logged

chenks

  • Kitizen
  • ****
  • Posts: 1106
Re: Regex question (again)
« Reply #1 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
Logged

burakkucat

  • Respected
  • Senior Kitizen
  • *
  • Posts: 38300
  • Over the Rainbow Bridge
    • The ELRepo Project
Re: Regex question (again)
« Reply #2 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.  ;)
Logged
:cat:  100% Linux and, previously, Unix. Co-founder of the ELRepo Project.

Please consider making a donation to support the running of this site.

Weaver

  • Senior Kitizen
  • ******
  • Posts: 11459
  • Retd s/w dev; A&A; 4x7km ADSL2 lines; Firebrick
Re: Regex question (again)
« Reply #3 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.
Logged