Simple regex question!



Hello :) I'm new to regular expressions so this is probably an easy

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.






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,




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

Similar Threads

Seemingly simple regex question 2
regex - should be simple 11
Regex Question 3
Regex Question 0
RegEx question 12
Regex question 2
Regex question 3
Regex question 2