Regex: pulling values out of this string

S

sklett

given one of these two string:
ProtocolRecord(0, 100, 100, arZeroMax , 1, 0, SKIP,0),
ProtocolRecord(0, 100, 100, arSetSet , 4, 0, DJMPNZSTOP, (void *)
&Protocol_TD[0])


I want to pull all the values out that are separated by commas and the last
value. I have no guarantee that there won't be a space before the comma
(IE: "0 , 0") they could also be: "0,0" or "0, 0"

I'm new to RegEx and learning as fast as I can. I cam up with this pattern,
but it's not getting a match:
@"ProtocolRecord\((\d)\w(\d)\w(\d)\w(\w+)\w(\d)\w(\d)\w(\w+)\w(\w+|\d)\)";

What I thought this would do was search for "ProtocolRecord(" then a digit,
then some word characters (' ' and ',')

I'm sure someone sees the error(s) here, is you do, can you clue me in?

Thanks for reading,
Steve
 
S

sklett

OK, I got one that works... but it could be vulnerable to some variations
that might make it fail.
Using this string as source: " ProtocolRecord (0, 100, 100,
arZeroMax , 1, 0,SKIP,0)"

I made this pattern:
@"ProtocolRecord\s*\((\d+)[,\s]*(\d+)[,\s]*(\d+)[,\s]*(\w+)[,\s]*(\d+)[,\s]*(\d+)[,\s]*(\w+)[,\s]*(\w+)\)";

So, 2 questions:
1) Could the pattern be made more compact somehow? I want to store the
matches so that I can get the values (I'm using this like sscanf() )
2) Is this the correct way to get the matches out?:
<code>
Regex reg = new Regex(pattern);
if( reg.IsMatch(line) == true)
{
string results = reg.Match(line).Groups[1].ToString();
}
</code>

Considering I have 8 matches I want to pull from the source string, it
doesn't seem correct that I need to call Match() each time to get access to
them. I'm sure that I'm doing this wrong... but there aren't many example
out there that show this.

Thanks for reading and any tips you might have,
Steve
 
A

AlanT

You are not allowing for either the white space or the or the commas.

The following will find all the parms. They are split into two named
groups <lastParm> and <parm>


Regex r = new
Regex(@"ProtocolRecord\((\s*(?<parm>[a-zA-Z0-9]+)\s*,)*(\s*(?<lastParm>.*)\s*)\)",

RegexOptions.ExplicitCapture);

Match m = r.Match(txtInput.Text);

if (m.Success) {

Group parmGroup = m.Groups["parm"];
foreach (Capture cap in parmGroup.Captures) {
rtbMain.AppendText(cap.ToString() +
System.Environment.NewLine);
}

Group lastParmGroup = m.Groups["lastParm"];
foreach (Capture cap in lastParmGroup.Captures) {
rtbMain.AppendText(cap.ToString() +
System.Environment.NewLine);
}

}


hth,
Alan.
 
S

sklett

Hi, thanks for the post!
AlanT said:
You are not allowing for either the white space or the or the commas.

Isn't [,\s]* basically saying "match 0 or more ',' or 0 or more \s" ?
I ask because when I run my pattern, I get all the parameters in separate
groups.

With that said, your approach looks MUCH cleaner, I will try yours!

Thanks again,
Steve

The following will find all the parms. They are split into two named
groups <lastParm> and <parm>


Regex r = new
Regex(@"ProtocolRecord\((\s*(?<parm>[a-zA-Z0-9]+)\s*,)*(\s*(?<lastParm>.*)\s*)\)",

RegexOptions.ExplicitCapture);

Match m = r.Match(txtInput.Text);

if (m.Success) {

Group parmGroup = m.Groups["parm"];
foreach (Capture cap in parmGroup.Captures) {
rtbMain.AppendText(cap.ToString() +
System.Environment.NewLine);
}

Group lastParmGroup = m.Groups["lastParm"];
foreach (Capture cap in lastParmGroup.Captures) {
rtbMain.AppendText(cap.ToString() +
System.Environment.NewLine);
}

}


hth,
Alan.
 
A

AlanT

My comment on the , and white space was in replay to your original, not
the follow up. Posting lag, sorry.

Alan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top