Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign variables with a regular expression

I'm looking for a method to assign variables with patterns in regular expressions with C++ .NET something like

String^ speed;
String^ size;

"command SPEED=[speed] SIZE=[size]"

Right now I'm using IndexOf() and Substring() but it is quite ugly

like image 343
Eric Avatar asked Sep 07 '25 13:09

Eric


1 Answers

String^ speed; String^ size;
Match m;
Regex theregex = new Regex (
  "SPEED=(?<speed>(.*?)) SIZE=(?<size>(.*?)) ",
  RegexOptions::ExplicitCapture);
m = theregex.Match (yourinputstring);
if (m.Success)
{
  if (m.Groups["speed"].Success)
    speed = m.Groups["speed"].Value;
  if (m.Groups["size"].Success)
    size = m.Groups["size"].Value;
}
else
  throw new FormatException ("Input options not recognized");

Apologies for syntax errors, I don't have a compiler to test with right now.

like image 120
Sparr Avatar answered Sep 09 '25 12:09

Sparr