简体   繁体   English

正则表达式完全匹配字符串

[英]regex exact match the string

I have below string which I split using regex and get id,id,roduct_clinical_studies__rimr,product__rim,product__v in an array. 我在下面的字符串中使用正则表达式拆分,并在数组中获取id,id,roduct_clinical_studies__rimr,product__rim,product__v。 This works for most of the cases. 这适用于大多数情况。 But for below string since clinical contains IN it fails.How can I do an exact match of IN/SELECT etc 但是对于下面的字符串,因为临床包含IN失败了,我该如何精确匹配IN / SELECT等

 id IN (SELECT id FROM  product_clinical_studies__rimr  WHERE product__rim CONTAINS {{this.product__v}})


String[] parseString(String filterInString) {
    return filterInString.replaceAll("(?i)CONTAINS|IN|SELECT|FROM|WHERE|(this\\.)|[(){}=,]|(\\s{2,})", " ").
            replaceAll("\\s+", " ").split("\\s");
}

If you can't identify by whitespace, then run this as two passes. 如果您不能通过空格识别,则将其运行两次。 First pass with all unique objects, second pass for just "IN". 第一次通过所有唯一对象,第二次通过“ IN”。

You could either get rid of the (?i) or add a word boundary around 您可以摆脱(?i)或在周围添加单词边界
the literals 文字

(?i)\\b(?:CONTAINS|IN|SELECT|FROM|WHERE)\\b|(this\\.)|[(){}=,]|‌​(\\s{2,})

If you need to match keyword/keyword/../.. runs , this probably works 如果您需要匹配keyword/keyword/../..运行,则可能可行
as well 以及

(?i)\\b(?:CONTAINS|IN|SELECT|FROM|WHERE)(?:/(?:CONTAINS|IN|SE‌​LECT|FROM|WHERE))*\\b‌​|(this\\.)|[(){}=,]|(‌​\\s{2,})

Expanded 扩展

    (?i)
    \b 
    (?:
         CONTAINS
      |  IN
      |  SELECT
      |  FROM
      |  WHERE
    )
    (?:
         /
         (?:
              CONTAINS
           |  IN
           |  SELECT
           |  FROM
           |  WHERE
         )
    )*
    \b 
 |  ( this\. )                    # (1)
 |  [(){}=,] 
 |  ( \s{2,} )                    # (2)

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

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