Regex Pattern for ignoring a custom escape character

Posted by user1517464 on Stack Overflow See other posts from Stack Overflow or by user1517464
Published on 2012-09-17T14:48:08Z Indexed on 2012/09/17 15:38 UTC
Read the original article Hit count: 350

Filed under:
|

I am trying to find a suitable regex for matching pair of custom characters in an input string. These custom characters are replaced by their corresponding html tags.

For e.g. The input string can have underscores in pairs to indicate words in bold. Hence, _Name_ outputs as <b>Name</b>

However if there is a genuine underscore in the string, it cannot be replaced by "bold" tags and has to be ignored. The genuine underscore has to be preceded by / (I couldn't find a better character, it could be one more underscore or hyphen or whatever). Any single or paired occurrance of this genuine underscore has to be ignored by regex.

So far I could come up with this regex:

var pattern = @"(?!/)_(.*?)(?!/)_";

But it fails in below input string:

_Tom_Katy/_Richard/_/_Stephan_and many users

It outputs as

<b>Tom</b>Katy/<b>Richard/_/</b>Stephan_and many users

Many Thanks in Advance, Pr

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex