正则表达式:匹配所有不在字符串开头或结尾的连字符或下划线

Regex: Match all hyphens or underscores not at the beginning or the end of the string

我正在编写一些需要将字符串转换为驼峰式大小写的代码。但是,我想在代码的开头允许任何 _-

我在这里使用正则表达式成功匹配了一个 _ 字符:

^(?!_)(\w+)_(\w+)(?<!_)$

当输入为:

pro_gamer #matched
#ignored
_proto 
proto_
__proto
proto__
__proto__
#matched as nerd_godess_of, skyrim
nerd_godess_of_skyrim

I recursively apply my method on the first match if it looks like nerd_godess_of.

我在添加 - 匹配项时遇到了麻烦,我认为只需将 - 添加到混合中就可以了:

^(?![_-])(\w+)[_-](\w+)(?<![_-])$

它匹配如下:

super-mario #matched
eslint-path #matched
eslint-global-path #NOT MATCHED.

我想了解为什么正则表达式无法匹配最后一个案例,因为它对 _.

工作正常

可以找到(几乎)完整的测试输入集here

_- 切换为 -_,这样 - 就不会像 a-z.

那样被视为范围操作

事实

^(?![_-])(\w+)[_-](\w+)(?<![_-])$

与 "eslint-global-path" 中的第二个连字符不匹配是因为锚点 ^ 将匹配限制为仅在第一个连字符上。此正则表达式为 "Match the beginning of the line, not followed by a hyphen or underscore, then match one or more words characters (including underscores), a hyphen or underscore, and then one or more word characters in a capture group. Lastly, do not match a hyphen or underscore at the end of the line."

下划线(但不是连字符)是单词 (\w) 字符这一事实完全搞乱了正则表达式。通常,您可能不想使用 \w,而是使用 \p{Alpha}\p{Alnum}(或 POSIX [[:alpha:]][[:alnum:]])。

试试这个。

r = /
    (?<=     # begin a positive lookbehind
      [^_-]  # match a character other than an underscore or hyphen
    )        # end positive lookbehind
    (        # begin capture group 1
      (?:    # begin a non-capture group
        -+   # match one or more hyphens
        |    # or
        _+   # match one or more underscores
      )      # end non-capture group
      [^_-]  # match any character other than an underscore or hyphen
    )        # end capture group 1
    /x       # free-spacing regex definition mode

'_cats_have--nine_lives--'.gsub(r) { |s| s[-1].upcase }
  #=> "_catsHaveNineLives--"

这个正则表达式按照惯例写成如下。

r = /(?<=[^_-])((?:-+|_+)[^_-])/

如果所有字母都是小写的,也可以这样写

'_cats_have--nine_lives--'.split(/(?<=[^_-])(?:_+|-+)(?=[^_-])/).
  map(&:capitalize).join
  #=> "_catsHaveNineLives--"

哪里

'_cats_have--nine_lives--'.split(/(?<=[^_-])(?:_+|-+)(?=[^_-])/)
  #=> ["_cats", "have", "nine", "lives--"]

(?=[^_-]) 是一个积极的前瞻,它要求进行拆分的字符后跟下划线或连字符以外的字符

你可以试试正则表达式

^(?=[^-_])(\w+[-_]\w*)+(?=[^-_])\w$

查看演示 here