gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 08:43:05 25 4
gpt4 key购买 nike

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

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

^(?!_)(\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

最佳答案

事实是

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

与“eslint-global-path”中的第二个连字符不匹配是因为 anchor ^ 将匹配限制为仅在第一个连字符上。这个正则表达式是,“匹配行的开头,后面没有连字符或下划线,然后匹配一个或多个单词字符(包括下划线),一个连字符或下划线,然后是捕获组中的一个或多个单词字符。最后, 不要匹配行尾的连字符或下划线。”

下划线(但不是连字符)是一个单词 (\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--"]

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

关于ruby - 正则表达式:匹配所有不在字符串开头或结尾的连字符或下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45779029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com