Parsing text

  • Thread starter Thread starter Q2004
  • Start date Start date
Q

Q2004

I have an arbitrary text as follow :

Repeat Time = 50 AB =200, Test Time = 40
A = 33 Ping Time = 30end remark

I used System.Text.RegularExpressions.Regex regex to query the value. Is
possible to query, say for example, "Repeat Time" and get the value 50
returned ?

What if I wants get all the value with the following output :

Repeat Time = 50
AB = 200
Test Time = 40
A = 33
Ping Time = 30

Using this :
string regexString = @"(?<variable>[^\s]+)\s*=\s*(?<value>[\d]+)";

doesn't work because it will always return the first string on the left of
"=" sign.

string sourceString = "Repeat Time = 50 AB =200, Test Time = 40 A = 33
Ping Time = 30end remark";

System.Text.RegularExpressions.Regex regex;
regex = new System.Text.RegularExpressions.Regex(regexString);
System.Text.RegularExpressions.Match match;
match = regex.Match(sourceString);
while(match.Success)
{
Console.WriteLine(match.Result("${variable}") + "=" +
match.Result("${value}"));
match = match.NextMatch();
}

Thanks.
 
I have an arbitrary text as follow :

Repeat Time = 50 AB =200, Test Time = 40
A = 33 Ping Time = 30end remark

I used System.Text.RegularExpressions.Regex regex to query the
value. Is possible to query, say for example, "Repeat Time" and
get the value 50 returned ?

What if I wants get all the value with the following output :

Repeat Time = 50
AB = 200
Test Time = 40
A = 33
Ping Time = 30

Using this :
string regexString =
@"(?<variable>[^\s]+)\s*=\s*(?<value>[\d]+)";

doesn't work because it will always return the first string on
the left of "=" sign.

Try this:

string regexString =
@"\s*(?<variable>.+?)\s*?=\s*?(?<value>\d+)(\s|,)*";

Hope this helps.

Chris.
 
Back
Top