这个正则表达式做了什么?

时间:2013-08-21 08:34:27

标签: regex

这是来自NHibernate的Automapping配置的字符串。我不知道它的作用。

return string.Format("{0}_", Regex.Replace(member.Name, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1_").ToUpper());

2 个答案:

答案 0 :(得分:6)

好的,我们分手吧。

//This is the start
([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))

[a-z](?=[A-Z]) //this means to match one lower case a-z followed by an uppercase A-Z
| //or
[A-Z](?=[A-Z][a-z]) //One uppercase A-Z followed by one uppercase and one lowercase a-z

//The replace
$1_ //Replace the match with "the match plus underscore". 
//aBxx would become a_Bxx and ABcxx would be A_Bcxx

Image http://f.cl.ly/items/3H2w3k0m1k03072h2s0y/orreMBA%202013-08-21%20kl.%2010.37.18.PNG

答案 1 :(得分:0)

让我们分解:

([a-z](?=[A-Z])  

第一部分匹配任何以大写字母结尾的小写字符

|[A-Z](?=[A-Z][a-z]))

或以大写字母结尾的任何大写字符

例如,这将匹配

AAb,第一个'A'是匹配或

AB

以'a'为匹配。

正则表达式正在使用http://www.regular-expressions.info/lookaround.html