473,473 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regex: pulling values out of this string

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
Apr 7 '06 #1
4 1839
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

"sklett" <sk****@mddirect.com> wrote in message
news:ec**************@TK2MSFTNGP04.phx.gbl...
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

Apr 7 '06 #2

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.

Apr 7 '06 #3
Hi, thanks for the post!
"AlanT" <al*******@users.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...

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.

Apr 8 '06 #4

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

Alan.

Apr 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Masahiro Ito | last post by:
I have attached a block of text similar to the type that I am working with. I have been learning a lot about Regex - it is quite impressive. I can easily capture bits of info, but I keep having...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
8
by: rjb | last post by:
Hi! Could somebody have a look and help me to optimize the code below. It may look like very bad way of coding, but this stuff is very, very new for me. I've included just few lines. Regex...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
3
by: spamsickle | last post by:
I have a Perl background, so some of what I know in other contexts is probably getting in the way of what I need to learn now. With that said, I'm having a problem getting my regex to work as I...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
4
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.