【问题标题】:How to match camel case identifiers with a Regular Expression?如何使用正则表达式匹配骆驼大小写标识符?
【发布时间】:2018-09-08 11:20:46
【问题描述】:

我需要匹配驼峰式大小写变量。我忽略了名称中带有数字的变量。

private const String characters = @"\-:;*+=\[\{\(\/?\s^""'\<\]\}\.\)$\>";
private const String start = @"(?<=[" + characters +"])[_a-z]+";
private const String capsWord = "[_A-Z]{1}[_a-z]+";
private const String end = @"(?=[" + characters + "])";

var regex =  new Regex($"{start}{capsWord}{end}", 
    RegexOptions.Compiled | RegexOptions.CultureInvariant) }

这非常适合匹配单个驼峰变量!但不是多个,也不是满足行尾的那个。我认为我的角色中的 $ 或 ^ 会允许它们匹配。

abcDef                                     // match 
notToday<end of line>                      // no match
<start of line>intheBeginning              // no match
whatIf                                     // match
"howFar"                                   // match
(whatsNext)                                // match
ohMyGod                                    // two humps don't match

我也试过像这样包装我的 capsWord "(capsWord)+" 但它也不起作用。 警告!正则表达式测试器使用此“(capsWord)+”进行在线匹配,因此不要从那里通过测试来验证和响应。

在我进行更改时,我的部署似乎没有得到更新,所以可能根本就没有问题。

除了行首问题之外,以下几乎可以工作。请注意,我注意到我不需要后缀部分,因为匹配以 [a-z] 内容结尾。

private const String characters = @"\-:;*+=\[\{\(\/?\s^""'\<\]\}\.\)$\>";
private const String pattern = "(?<=[" + characters + "])[_a-z]+([A-Z][a-z]+)+";

abcDef                                     // match 
notToday<end of line>                      // match
<start of line>intheBeginning              // no match
whatIf                                     // match
"howFar"                                   // match
(whatsNext)                                // match
ohMyGod                                    // match

所以,如果有人能解决,请告诉我。

我还将其他字符简化为更简洁的表达式,但仍然存在从行首匹配的问题。

private const String pattern = "(?<=[^a-zA-Z])[_a-z]+([A-Z][a-z]+)+";

【问题讨论】:

    标签: c# regex camelcasing


    【解决方案1】:

    可以匹配前缀和后缀之间的空位来拆分驼峰式标识符

    (?<=[_a-z])(?=[_A-Z])
    

    前缀包含小写字母,后缀包含大写字母。


    如果要匹配 camelCase 标识符,可以使用

    (?<=^|[^_a-zA-Z])_*[a-z]+[_a-zA-Z]*
    

    它是如何工作的:

    (?<=                Match any position pos following a prefix exp    (?<=exp)pos
        ^               Beginning of line
        |               OR
        [^_a-zA-Z]      Not an identifier character
    )
    _*                  Any number of underlines
    [a-z]+              At least one lower case letter
    [_a-zA-Z]*          Any number of underlines and lower or upper case letters
    

    所以,它基本上说:匹配一个序列,可选地以下划线开头,后跟至少一个小写字母,可选地后跟下划线和字母(大写和小写),并且整个内容之前必须有一个开头行或非标识符字符。这对于确保我们不仅匹配以大写字母(或下划线和大写字母)开头的标识符的结尾是必要的。

    var camelCaseExpr = new Regex("(?<=^|[^_a-zA-Z])_*[a-z]+[_a-zA-Z]*");
    MatchCollection matches = camelCaseExpr.Matches("whatIf _Abc _abc howFar");
    foreach (Match m in matches) {
        Console.WriteLine(m.Value);
    }
    

    打印

    whatIf
    _abc
    howFar
    

    【讨论】:

    • 我误解了我的需求。我实际上不需要拆分它们,而是匹配它们。感谢您的建议。
    【解决方案2】:

    今天遇到了同样的问题,什么对我有用:

    \b([a-z][a-z0-9]+[A-Z])+[a-z0-9]+\b
    

    注意:这是针对 PCRE 正则表达式的

    解释:

    `(` group begin
    
    `[a-z]` start with a lower-case letter
    
    `[a-z0-9]+` match a string of all lowercase/numbers
    
    `[A-Z]` an upper-case letter
    
    `)+` group end; match one or more of such groups.
    

    以更多的小写/数字结尾。 \b 用于单词边界。

    就我而言,_camelCaseIdent_s 在单词之间只有一个大写字母。 所以,这对我有用,但如果你可以拥有(或想要匹配)多个 大写字母,你可以做类似[A-Z]{1,2}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 2015-12-13
      • 2011-01-07
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      相关资源
      最近更新 更多