Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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'?

Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte
Posted

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.

Gamer extraordinaire. Programmer wannabe.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...