Home All Groups Group Topic Archive Search About

Simple regex question!

Author
6 Apr 2006 7:01 PM
sb
Hello :)  I'm new to regular expressions so this is probably an easy
question.

I have one large string that consists of multiple "lines" that are "\r\n"
delimited.  How can I use regex to parse each line of the string and create
two groups: 1 that contains all lines that start with "X_" and 1 that
contains all lines that start with "Y_"?
I think I've got the regex pattern right but I can't seem to figure out how
to access the index/length properties of each line that was found in group.
The regex pattern I'm using is below:

Match m = Regex.Match(mystring, "^(?<plines>[X][_])|(?<clines>[Y][_]))",
RegexOptions.Multiline | RegexOptions.ExplicitCapture);
....

The bottom line is that I need to know the index/length of each "line" that
starts with "P_" or "C_".  I know I can do this without using regex but I'm
simplifying things quite a bit here.

I hope that make sense.


TIA!
sb

Author
7 Apr 2006 3:45 AM
Cerebrus
Hi,

When posting questions about Regex, it is always useful to give some
sample text, then show what you *would* like to match, and what you
would *not* like to match.

In this case, I had to make up my own sample text :

X_Lorem ipsum dolor sit amet.
Y_Lorem ipsum dolor sit amet.
X_Lorem ipsum dolor sit amet.
X_Lorem ipsum dolor sit amet.
Y_Lorem ipsum dolor sit amet.

The Regex pattern that you are using (
^(?<plines>[X][_])|(?<clines>[Y][_])  ) selects only the X_ and the Y_
part of each line. You mentioned that you want to select the whole line
and determine it's length. So you need to select any and all text after
the X_ or Y_ until the end of the line.

The Regex I used was : ^(?<plines>X_.*)|(?<clines>Y_.*)$
RegexOptions required : MultiLine.

After this, use the Length and Index properties of the Match to get the
information you need.

Hope this helps,

Regards,

Cerebrus.
Author
7 Apr 2006 5:14 AM
sb
Thanks!  I see where my problem was now.

-sb

Show quote
"Cerebrus" <zorg***@sify.com> wrote in message
news:1144381556.884612.219110@i39g2000cwa.googlegroups.com...
> Hi,
>
> When posting questions about Regex, it is always useful to give some
> sample text, then show what you *would* like to match, and what you
> would *not* like to match.
>
> In this case, I had to make up my own sample text :
>
> X_Lorem ipsum dolor sit amet.
> Y_Lorem ipsum dolor sit amet.
> X_Lorem ipsum dolor sit amet.
> X_Lorem ipsum dolor sit amet.
> Y_Lorem ipsum dolor sit amet.
>
> The Regex pattern that you are using (
> ^(?<plines>[X][_])|(?<clines>[Y][_])  ) selects only the X_ and the Y_
> part of each line. You mentioned that you want to select the whole line
> and determine it's length. So you need to select any and all text after
> the X_ or Y_ until the end of the line.
>
> The Regex I used was : ^(?<plines>X_.*)|(?<clines>Y_.*)$
> RegexOptions required : MultiLine.
>
> After this, use the Length and Index properties of the Match to get the
> information you need.
>
> Hope this helps,
>
> Regards,
>
> Cerebrus.
>

AddThis Social Bookmark Button