简体   繁体   中英

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. 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

 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".

You could either get rid of the (?i) or add a word boundary around
the literals

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

If you need to match keyword/keyword/../.. runs , this probably works
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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