471,320 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,320 software developers and data experts.

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

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.
Dec 14 '05 #1
2 2592
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"James" <ja********@hotmail.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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.

Dec 14 '05 #2
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.
"James" <ja********@hotmail.com> a écrit dans le message de
news:%2***************@tk2msftngp13.phx.gbl...
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.

Dec 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Andy Crawford | last post: by
21 posts views Thread by nicholas | last post: by
4 posts views Thread by Gérard Talbot | last post: by
10 posts views Thread by Kurda Yon | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.