简体   繁体   中英

Regex to find instances of String.Compare with two parameters

I'm looking for instances in my code where I have forgotten to include a 'true' for the third parameter, which lets it be case insensitive.

The code would either look like String.Compare("stringA", varB) or String.Compare(varA, "stringB", true) and I would want to use regex to find all instances of cases where I only have used two parameters.

So far I came up with this: String.Compare\\([^,\\n]*((,[^,\\n]*?))\\) , which works for simple cases but I'm not sure how to deal with things such as if the parameter has a .ToString() at the end of it.

If anybody has any advice to how I could improve my regex or even a completely different but more efficient solution to my issue that would be great!

EDIT: Just wanted to mention that sometimes it could broken up into different lines:

if (String.Compare(varA.ToString(),
varB.ToString().Trim(),
StringComparison.OrdinalIgnoreCase) != 0)

There might be a few things to improve. If you don't need the capturing groups you can omit them turning them into non capturing (?: . There is also a double capturing group (( where you could omit the outer one.

You can escape the dot after String. or else it would match any char except a newline.

In your negated character class you match not a , or \\n , but if there can not be a closing parenthesis you might also add that to it like [^,\\n)] .

You could also remove the question mark after [^,\\n]*? which will make it non greedy.

Your expression might look like:

String\.Compare\([^,\n)]*(?:,[^,\n)]*)\)

If you want to match all non whitespace chars that follow starting with a dot, you could add an optional non capturing group:

String\.Compare\([^,\n)]*(?:,[^,\n)]*)\)(?:\.\S+)?

If String should not be part of a larger word, you might prepend a word boundary \\bString

Regex demo

这应该可行,并且不要忘记忽略大小写:

String\.Compare\([^,]*(?:,[^,()]*)(\(\))?\);$

Assuming that this is a one-time task I would do it with Notepad++ in several steps.

  1. Mark all lines containing String.Compare . Use "Mark" tab of Find and replace dialogue, ensure "Bookmark line" is ticked.
  2. Inverse bookmarks. Use menu => Search => Bookmarks => Inverse bookmarks. Now all lines that do NOT contain String.Compare are marked.
  3. Do a mark lines with the regular expression String\\.Compare\\([^,\\n]*,[^,\\n]*, *true\\);$ . This add marks on the string compare lines that have the wanted true . Note the final ;$ in the regular expression. It ensures that the true is indeed the third parameter of the call and is not matching a true earlier in the line. This does not need anything extra for the \\.ToString() clauses
  4. Inverse bookmarks.
  5. Now have marks on all the String.Compare lines that do not have the wanted true as the third parameter on the line.

To summarise: Find (and mark) the lines that are OK, then inverse the marks to highlight the lines that may need attention.

In response to the changed question, change the regular expression in step-3 to be String\\.Compare\\([^,]*,[^,]*,\\s*true\\)(\\s*!=\\s*0\\))?$ or even String\\.Compare\\([^,]*,[^,]*,\\s*StringComparison\\.\\w+\\)(\\s*!=\\s*0\\))?$ .

This change allows for the third parameter to be a StringComparison enumeration and also for the comparison result to be compared against zero.

Changing the regular expression to String\\.Compare\\([^,]*,[^,]*,\\s*(true|StringComparison\\.\\w+)\\)(\\s*!=\\s*0\\))?$ should allow either true or a StringComparison enumeration as the third parameter. Generally I am against using such complex regular expressions. They get difficult to understand and difficult to enhance. For a task such as in this question and the style of solution given in this answer it may be better to perform two or more mark lines operations with different expressions at step-3 above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM