Regex multiple occurrences after positive lookbehind

Viewed 70

I am trying to parse comments in my source file and I am stuck with reg exp.

/************************************************************
 * Code formatted by SoftTree SQL Assistant © v10.1.278
 * Time: 23.04.2021 11:57:15
 ************************************************************/

/*******************************************
 * 
 * some string 1
 * 
 * some string 2
 * 
 * some string 3
 * 
*******************************************/

I am trying to extract text

some string 1
some string 2
some string 3

If my Reg Exp looks like

/( \* ([\S ]+)\n)/g

it catches strings from the first comment block. So, I am trying to make it to process only second comment block with lookbehind:

/(?<=\/[\*]{43}\n)( \* ([\S ]+)?\n)/g

But with this reg exp I am getting only line *, that is going after /*******************************************.

How to combine lookbehind with simple reg exp /( \* ([\S ]+)\n)/g to catch lines with strings that are comes after /*******************************************?

I mean, these strings:

some string 1
some string 2
some string 3
1 Answers

You can try the following regex:

(?s)(?<=/\*{43}\r?\n(?:(?!/\*{43}\r?\n \*).)*?\r?\n \* )\S(?-s).*

See the regex demo.

Details

  • (?s) - an inline RegexOptions.Singleline "dotall" modifier
  • (?<=/\*{43}\r?\n(?:(?!/\*{43}\r?\n \*).)*?\r?\n \* ) - a positive lookbehind that matches a location that is immediately preceded with:
    • /\*{43} - / and 43 asterisks
    • \r?\n - a linebreak, CRLF or LF
    • (?:(?!/\*{43}\r?\n \*).)*? - any one char, zero or more but as few as possible occurrences, that does not start a /+43 asterisks, a line break, a space and an asterisk char sequence
    • \r?\n \* - a CRLF or LF line ending and then a space, asterisk, space
  • \S(?-s).* - any non-whitespace char and then any zero or more chars other than a line feed char as many as possible (the "dotall" modifier effect is turned off with the (?-s) modifier).
Related