How C# Regex to express "IDENTIFIER = ON/OFF"

  • Thread starter Thread starter James
  • Start date Start date
J

James

User has a input as "SwitchName = ON" or "SwitchName = OFF", how to use C#
Regex to get SwitchName and the value?

Regex.Match(line, @"(\S+) = {ON|OFF}") does not work.
 
James,

Why bother using a regular expression? Why not just parse based on the
equal sign and then assign to a key/value pair?

Hope this helps.
 
Use the following regex : ^(\w+?)\s*=\s*(ON|OFF)\s*$
Match m=Regex.Match("mySwitch = ON",@"^(\w+?)\s*=\s*(ON|OFF)\s*$");
MessageBox.Show("switchname = "+m.Groups[1]);
MessageBox.Show("switch = "+m.Groups[2]);

Hope it helps,

Ludovic SOEUR.
 
Back
Top