开发者

What is this Regex doing: new Regex(@"(?<!\\),");

Regex rx = new Regex(@"(?<!\\\\),");
String test = "OU=James\\, Brown,OU=Test,DC=Internal,DC=Net";

This works perfectly, but I want to understand it. I've been gooling without success. Can somebody give me a word or phra开发者_高级运维se that I can use to look this up and understand it.

I would have thought that it should be written like this:

 new Regex(@"(\\\\)?,");

I've seen the (?zzzzzz) syntax before. It's the <! part that I'm stumped by.


(?<!…) is a negative look-behind assertion. In your regex

(?<!\\\\),

the , matches a comma obviously. The \\\\ matches 2 backslashes. Then (?<!\\\\), matches any commas not preceeded by 2 backslashes.

Therefore it will match the , before the OU and DC, but not between James and Brown:

OU=James\\, Brown,OU=Test,DC=Internal,DC=Net
                 ^       ^           ^


The <! part indicates a negative lookbehind. The rest of the expression (just a comma) matches only if it's not preceded by a backslash (or two backslashes, depending on whether the title or the body of your question is the accurate one).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜