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 2,542 times
2
-
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 [`(? – 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
6
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
- 484,719
- 26
- 302
- 397
-
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