为什么此 Perl Regex 单行代码没有使用修饰符使完整结果大写\U
?
我预计MY_NICE_WORD
。
$ perl -pe 's/(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/\U$1_$2\E/g' <(echo 'myNiceWord HTTPHeader')
my_Nice_Word HTTP_Header
根据文档,
The following escape sequences are available in constructs that interpolate, but not in transliterations.
\l lowercase next character only
\u titlecase (not uppercase!) next character only
\L lowercase all characters till \E or end of string
\U uppercase all characters till \E or end of string
\F foldcase all characters till \E or end of string
\Q quote (disable) pattern metacharacters till \E or
end of string
\E end either case modification or quoted section
(whichever was last seen)
来源:man --pager 'less -p "The following escape sequences are available in constructs that interpolate, but not in transliterations."' perlop
替换文本不是在插值上下文中吗?
工作示例
根据我的理解,遵循文档后我可以提供这个工作示例:
$ perl -pe 's/(hello)/\U$1\E/g' <(echo hello)
HELLO
正则表达式来自https://stackoverflow.com/a/1176023/1236128
-> 这涵盖的重要情况:HTTPHeader 不会变成 h_t_t_p_header,而是HTTP_Header
(或大写的 HTTP_HEADER)