Regex: how do you get to match __X__Y__ ?

pasu857

Newcomer
Joined
Sep 20, 2003
Messages
2
I am trying to get Regex to match something like this:

__X__Y__
__X_x__Y__
etc

in particular, I want the content between those "__", so in the first example I will get X and Y, while in the second example I should get "X_x" and Y.

So far I have got to the following Regex:

Regex X = new Regex(@"__(?<object>[\w]+?)__");

This match __X__ and __X_x__, etc, and I can refer the content through the group name "object". But I don't know how to go on from there.

Is there someone out there that can point me in the right direction?

Many Thanks.
 
Not understanding the question...

I'm not quite catching the question. Are you capturing the 'Y' as well as the 'X' and 'X_x' matches with that expression? Or perhaps you are purposefully omitting the 'Y'?
 
I am trying to capture all the X, Y and X_x (those are just examples).

I want to capture all the content in between those "__". Sorry for the confusion.
 
Edit: Made a few alterations to the regular expression

Maybe something like this;

"^(?<=_{2})(?<name>(\w+|\w+_{1}\w+))(?=_{2})$"

Basically it's saying, get a match for any word or word_word between __ and __ starting at the beginning of the string and going until the end.

It may not work exactly (regular expressions hardly do on the first try), but the key thing to note here is the either/or symbol | which returns a match of either expression.

Hope this helps.
 
Back
Top