简体   繁体   English

REGEX 在两个字符之间获取第 N 个单词

[英]REGEX to get Nth word between two characters

I am trying to get a regex to use in Data Studio, what I am trying to do is be able to pull out the following into custom fields so I would need 4 regex's for each one我正在尝试在 Data Studio 中使用正则表达式,我想要做的是能够将以下内容提取到自定义字段中,因此我需要为每个字段使用 4 个正则表达式

STRING = first_Product (21) » second_Product_3 (64) » third_Product (53) » fourth_Product_4 (21) STRING = first_Product (21) » second_Product_3 (64) »third_Product (53) »fourth_Product_4 (21)

into .进入

custom_field_1 = first_product. custom_field_1 = first_product。

custom_field_2 = second_product. custom_field_2 = second_product。

custom_field_3 = third_product. custom_field_3 =third_product。

custom_field_4 = fourth_product. custom_field_4 = 第四个产品。

To get the first I am using为了获得我正在使用的第一个

.+?(?=\()

To get the second one I am able to use为了获得第二个我可以使用

    \»(.*?)\( 

Would anyone be able to helo with this有没有人可以用这个

You could use a regex with a quantifier like {2} {3} to get the specific matches from capture group 1.您可以使用带有{2} {3}等量词的正则表达式从捕获组 1 中获取特定匹配项。

^(?:(?:^|»\s+)(.*?)\s*\(\d+\)(?:\s+|$)){2}

The pattern matches:模式匹配:

  • ^ Start of string ^字符串开始
  • (?: Non capture group to match as a whole part (?:非捕获组作为整体匹配
    • (?:^|»\\s+) Assert the start of the string or match » and 1+ whitespace chars (?:^|»\\s+)断言字符串的开头或匹配»和 1+ 个空白字符
    • (.*?) Capture group 1 , match 1+ times any char as least as possible (.*?)捕获组 1 ,尽可能匹配任何字符的 1+ 次
    • \\s*\\(\\d+\\) Match optional whitespce chars and 1+ digits between parenthesis \\s*\\(\\d+\\)匹配可选的 whitespce 字符和括号之间的 1+ 位数字
    • (?:\\s+|$) Match either 1+ whitespace chars or assert the end of the string (?:\\s+|$)匹配 1+ 个空白字符或断言字符串的结尾
  • ){2} Close the non capture group and repeat n times (in this example 2 times) ){2}关闭非捕获组并重复 n 次(在本例中为 2 次)

Regex demo正则表达式演示

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

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