-3

This is a very basic regex question.

Suppose I have lots of string constants throughout my code (enclosed with double quotations). Some of these strings contain class substring like the "declassification" constant in this example:

public class Names {
    string text = "declassification";
    string classified = "foo";
}

All 3 lines contain class, but only "declassification" is interesting to me because it's between double quotations.

What I did:

I found this question: Regex.Matches c# double quotes. It tells me how to get everything in a double quotations like this: \"(.*?)\". But I need to search for a specific string anywhere between the double quotations.

This question: Extract the values between the double quotes using regex also tells me to use this: @"""New[^"":]+:[^""]+""" which doesn't work for me.

Find Results:

Wrong:

public class Names {

string text = "declassification";

string classified = "foo";

Expected:

string text = "declassification";

Question:

When I search with Visual Studio Find Tool, how can I only search through the string constants and ignore the rest of the files?

Bizhan
  • 13,495
  • 9
  • 53
  • 83
  • There are lots of questions with answers on Stack Overflow describing how to use regex to find text within double quotes. E.g. marked duplicates. See also https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean. Note that in general, you are going to have difficulty using regex with matched delimiters like quotes if there is the possibility of nested delimiters (like escaped quotes within a string) anywhere in the search text. – Peter Duniho Jan 20 '20 at 21:28
  • @PeterDuniho although that reference contains my question but I think this question should not be closed just because I, for instance, couldn't find anything on stackoverflow when I searched tens of different wordings related to this question. I spent an hour reading a blogpost about basic regex to find this solution but We are lazy and we always don't have time to read through long documentations :) I don't know if closing this question prevents it from appearing in search results or not, but I think it will greatly help in the future to find a useful piece of code – Bizhan Jan 20 '20 at 22:08
  • _"We are lazy"_ -- if and when the guidance on the Stack Overflow community, such as that found in the Tour and the Help sections, indicate that the author of a question simply being lazy is sufficient justification for leaving that author's question open, in spite of there being adequate information in previously-posted questions and answers to answer their question, then the fact that you're lazy would be relevant. Until then, no. – Peter Duniho Jan 20 '20 at 22:12
  • If you place your mouse cursor over the downvote button, you'll see a brief description of reasons that a question might be downvoted. The question both shows no evidence of prior research, and does not provide information beyond what is already available in ample quantity both on Stack Overflow and the documentation. So it is not surprising to me that the question has garnered a couple of downvotes and no upvotes. You can find more information in the [Stack Overflow Help section](https://stackoverflow.com/help). – Peter Duniho Jan 21 '20 at 16:20

1 Answers1

1

Hint: .* means any number of any characters

With regex if you search \"class\" it exactly matches "class".

To match "<anything>class<anything>" add any number of any characters before and after class:

\".*class.*\"

To match '<anything>class<anything>' as well (such as javascript strings) use this:

["'](.*)(class)(.*)["']
Bizhan
  • 13,495
  • 9
  • 53
  • 83