I need a regex
to search for the string SQLHELPER
that ignores commented code (single line comment or multi line comments). I am searching in visual studio.
Asked
Active
Viewed 5,193 times
4
-
It is too hard to determine if a string is within multi line comments using regex – Jacob Boertjes Jul 12 '18 at 18:36
-
Possible duplicate of [Exclude comments when searching in Visual Studio](https://stackoverflow.com/questions/11314366/exclude-comments-when-searching-in-visual-studio) – Jacob Boertjes Jul 12 '18 at 18:46
-
@JacobBoertjes : Yes , this might be a duplicate , but original post does not answer , how to exclude multi line comments , I had checked that post already.But I am looking for a single REGEX which can ignore both Single and multi line comments – Sagar Jul 12 '18 at 18:51
-
After reading to comments on the other question, I would conclude that it is not possible to exclude multi line comments. Unfortunately the answers provided there are the best you are going to get. – Jacob Boertjes Jul 12 '18 at 18:52
-
1Try [`(?<!^[\p{Zs}\t]*//.*)(?<!/\*(?:(?!\*/)[\s\S\r])*?)\bSQLHELPER\b`](http://regexstorm.net/tester?p=%28%3f%3c!%5e%5b%5cp%7bZs%7d%5ct%5d*%2f%2f.*%29%28%3f%3c!%2f%5c*%28%3f%3a%28%3f!%5c*%2f%29%5b%5cs%5cS%5cr%5d%29*%3f%29%5cbSQLHELPER%5cb&i=SQLHELPER%0d%0a%2f%2f+SQLHELPER%0d%0aSQLHELPER%0d%0aSQLHELPER+%2f*+SQLHELPER++%2f*+SQLHELPER%0d%0ahere+SQLHELPER*%2f+++SQLHELPER%0d%0a%2f*+SQLHELPER+*%2f+SQLHELPER&o=m) – Wiktor Stribiżew Jul 12 '18 at 19:45
-
Will Visual Studio accept a lookbehind of non-fixed width though? – Jacob Boertjes Jul 12 '18 at 19:52
-
@WiktorStribiżew , THANKS Wiktor , it solved my problem , now it seaches in single and multi line comments also – Sagar Jul 17 '18 at 10:16
-
So, shall I post an answer? You wanted to match outside of them, right? – Wiktor Stribiżew Jul 17 '18 at 10:18
-
@WiktorStribiżew yes , post it as answer , it solves my problem , i tried searching for word SQLHELPER in all .CS file in VS , and it did not list commented out code ( single or multi ) , which was my requirment, thanks again – Sagar Jul 17 '18 at 10:20
1 Answers
10
You may use
(?<!^[\p{Zs}\t]*//.*)(?<!/\*(?:(?!\*/)[\s\S\r])*?)\bSQLHELPER\b
See the regex demo.
Details
(?<!^[\p{Zs}\t]*//.*)
- a negative lookbehind that fails the match if, immediately to the left of the current location, the following pattern does not match:^
- start of line[\p{Zs}\t]*
- any 0+ horizontal whitespaces//
- a//
substring.*
- any 0+ chars other than line break chars
(?<!/\*(?:(?!\*/)[\s\S\r])*?)
- - a negative lookbehind that fails the match if, immediately to the left of the current location, the following pattern does not match:/\*
- a/*
substring(?:(?!\*/)[\s\S\r])*?
- (tempered greedy token) any char (matched with[\s\S\r]
), 0 or more repetitions but as few as possible (due to*?
) that does not start a*/
substring (due to the(?!\*/)
negative lookahead)
\bSQLHELPER\b
- a whole wordSQLHelper
(\b
are word boundaries).

Wiktor Stribiżew
- 607,720
- 39
- 448
- 563
-
If I use this in java I get :> java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 48 – Fergal Fitz Sep 21 '20 at 16:30
-
1@FergalFitz Of course, it is a JS ECMAScript 2018+ compliant regex. You can only use it in .NET, JS or Python PyPi regex. – Wiktor Stribiżew Sep 21 '20 at 16:39