-2

I have this piece of code but have no idea what it is supposed to be matching. I have looked at many different sites to try and learn the keywords, but I just don't understand regex.

string key = @"^(.*)\s*=\s*(.*)\s*$";
Match value = Regex.Match(line, key);
Josh Davis
  • 90
  • 8
  • 1
    it just captures the key, value pair into two separate groups. Here http://regex101.com/r/mL4sH7/4 see the explanation at the right side. If you don't want to capture the last following spaces then add a `?` inside the second capturing group like `(.*?)` – Avinash Raj Oct 02 '14 at 13:56
  • 3
    http://regex101.com can explain a regex given a regex. – Matthew Oct 02 '14 at 13:57
  • 2
    When in doubt use [regex101](http://regex101.com/r/dN4rD1/1) and its wonderful "explanation" panel. – user703016 Oct 02 '14 at 13:57

1 Answers1

3

This looks for the start of a line (^), finds any number of characters ((.*)), followed by some whitespace (\s*) an equal sign (=) some more whitespace (\s*) and any number of characters ((.*)) and the end of line ($)

Some valid example lines:

a=a
abc   =   xyz
value=5

etc

Hogan
  • 63,843
  • 10
  • 75
  • 106
  • I was able to go back to the file it is taking a line from and see what it is talking about now thank you very much for your help. Now I'm just really confused why he did it this way. – Josh Davis Oct 02 '14 at 14:31
  • he did it because it is an easy way to parse a line. parsing by hand and getting as much error checking would probably take about 10 lines instead of 2. – Hogan Oct 02 '14 at 15:06
  • yea I got to see the actual file he would be using and it makes allot of since why he did it that – Josh Davis Oct 03 '14 at 18:50