Search code examples
c#.netregex.net-2.0trim

c# read value from file and ignore everything except for value


I have a program that I need to have a config file with value to be displayed in my program. Inside my text file I have Wireless = 1 & Cradle = 2.

In my program I will have a label populate the release number only and not the other characters.


Solution

  • private string searchFile(String path, String searchText)
    {
          string regex=@"(?i)(?<="+searchText+@"\s*=\s*)\d+";
          return Regex.Match(File.ReadAllText(path),regex).Value;//version number
    }
    

    This is what I tried and it gives the correct output

    string s="Wireless = 1 Cradle = 2";
    Regex.Match(s,@"(?i)(?<=Wireless\s*=\s*)\d+").Value;//1