简体   繁体   English

正则表达式拆分文本。 括号中带空格的特殊情况

[英]Regex to split apart text. Special case for parentheses with spaces in them

I am trying to split a field by delimiter in LookML.我正在尝试通过 LookML 中的分隔符拆分字段。 This field either follows the format of:该字段遵循以下格式:

  1. Managers (AE)经理(AE)
  2. Managers (AE - MM)经理(AE - MM)

I was able to split to first case using this我能够使用这个拆分到第一个案例

sql: case
      when rlike (${user_role_name}, '^.*[\\(\\)].*$') then split_part(${user_role_name}, ' ', -1)

However, I haven't been able to get the 2nd case to do the same.但是,我无法让第二个案例做同样的事情。 It's in a case statement so I am going to add another when statement, but am not able to figure out the regex for parentheses that contains spaces.它在 case 语句中,因此我将添加另一个 when 语句,但无法找出包含空格的括号的正则表达式。

Thanks in advance for the help!在此先感谢您的帮助!

By "split" the string, I think you mean you want to extract the part in parentheses, right?通过“拆分”字符串,我认为您的意思是要提取括号中的部分,对吗?

I would do this using a regex substring method.我会使用正则表达式子字符串方法来做到这一点。 You didn't mention what warehouse you're using, and the syntax will vary a little, but on snowflake that would look like:你没有提到你正在使用什么仓库,语法会有所不同,但在雪花上看起来像:

regexp_substr(${user_role_name}, '\\([^)]*\\)')

So, for example, with the inputs you gave:因此,例如,使用您提供的输入:

select regexp_substr('Managers (AE)', '\\([^)]*\\)')
union all
select regexp_substr('Managers (AE - MM)', '\\([^)]*\\)')
result结果
(AE) (AE)
(AE - MM) (AE - 毫米)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM