-2

I want to run regex search in VSCode for a file (among hundreds of other files) containing multiple strings appearing in a specific order, ignoring line breaks.

Example scenario: There's a file containing the below code:

class Bicycle {

    public int gear; 
    public int speed; 

    public Bicycle(int gear, int speed) { 
        this.gear = gear; 
        this.speed = speed; 
    } 

    public void applyBrake(int decrement) { 
        speed -= decrement; 
    } 
}

And I want to run a search to find this file using the following strings in a regex search:

"class Bicycle"
"this.gear = gear;"
"decrement; 
    }"

If possible, please provide a solution that matches the strings even if they are out of order, like:

"this.gear = gear;"
"decrement; 
    }"
"class Bicycle"
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
shaqqy
  • 1
  • 3

2 Answers2

0

Try this:

var files = Directory.GetFiles(@"C:\path\to\files", "*.cs", SearchOption.AllDirectories);
foreach(var path in files)
{
    string content = File.ReadAllText(path);
    if(
    content.Contains("class Bicycle")
    && content.Contains("this.gear = gear;")
    && content.Contains("decrement; \r\n    }")
    )
    {
        Console.WriteLine("found: " + path);
    }
}
0

Try ^(?=.*class\s*Bicycle)(?=.*this\.gear\s*=\s*gear\s*;)(?=.*decrement\s*})
enable dot-all flag