首页 > 解决方案 > 正则表达式在 Redshift 中将 CamelCase 转换为蛇形案例

问题描述

我正在尝试将 CamelCase 转换为蛇形大小写或regex在 SQL (AWS Redshift) 中使用分隔符分隔。所以像

regexp_replace(MyString, '([A-Z]+)', '-$1')

除了我需要不在字符串的开头指定。马上,

MyString->-my-string而不是my-string.

我该怎么做呢?

标签: sqlregexamazon-redshift

解决方案


Match and capture any char before the uppercase letters, and restore it using another backreference in the replacement pattern:

regexp_replace(MyString, '(.)([A-Z]+)', '$1-$2')
                          ^^^            ^^^^^

See the regex demo.

I understand you already LOWER the result after the regex replacement.


推荐阅读