0
    String query = "Select Count(distinct c.requestId)  FROM Abc1Value c  WHERE 1=1 and c.templateName is NULL "
            +" AND (c.quickStatus IS NULL OR c.quickStatus = 'S') "
            + " AND (c.sCode='MYCODE'  OR exists (SELECT b.dseaReqId FROM drstSShareValue b WHERE b.dseaReqId=c.requestId and b.sCode='MYCODE'))  "
            + " AND  (upper(c.licenseNo) like '%"12548"%' or upper(c.docLicenseNo) like '%""%' or upper(c.uncontrolledLicense) like '%""%' or upper(c.nonAuthNo) like '%""%' or upper(c.reAuthNo) like '%""%') "
            + " and  upper(c.grantedByCtryCode) like '%US%' ";

I want to extract all "like string" i.e.

  1. like '%"12548"%'
  2. like '%""%'
  3. like '%""%'
  4. like '%""%'
  5. like '%""%'

I am using regex in java (?i)(\\s)+like(\\s)*('%\")(.+?)(\"%') when i get the group it does not return correct result. Please suggest.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Try `"(?i)\\s+like\\s+'%\".*?\"%'"`. –  Feb 27 '21 at 02:34
  • The java code in your question does not compile. If you **really** need the embedded double quote characters (`"`), then you must escape them like so: `\"` However I don't think you need them so you can simply remove them. – Abra Feb 27 '21 at 11:21
  • _I want to extract all "like string"_ Does that mean you also want to extract `like '%US%'`? You didn't include that in your expected output. – Abra Feb 27 '21 at 12:26

3 Answers3

1

You can use the regex, (?i)like\s+'%.*?%' which means:

  • (?i) matches the remainder of the pattern in a case-insensitive manner e.g. it enables matching all of the following words: like, LIKE, Like etc.
  • \s+ matches whitespace characters one or more times.
  • .*? matches lazily, any character any number of times.

Demo:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String query = """
                Select Count(distinct c.requestId)  FROM Abc1Value c  WHERE 1=1 and c.templateName is NULL
                 AND (c.quickStatus IS NULL OR c.quickStatus = 'S')
                 AND (c.sCode='MYCODE'  OR exists (SELECT b.dseaReqId FROM drstSShareValue b WHERE b.dseaReqId=c.requestId and b.sCode='MYCODE'))
                 AND  (upper(c.licenseNo) like '%"12548"%' or upper(c.docLicenseNo) like '%""%' or upper(c.uncontrolledLicense) like '%""%' or upper(c.nonAuthNo) like '%""%' or upper(c.reAuthNo) like '%""%')
                 AND  upper(c.grantedByCtryCode) like '%US%'
                """;
        Matcher matcher = Pattern.compile("(?i)like\\s+'%.*?%'").matcher(query);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

Output:

like '%"12548"%'
like '%""%'
like '%""%'
like '%""%'
like '%""%'
like '%US%'

Additional demo: check this out at regex101.

Note: In order to keep query look clean, I've used the Text Blocks (or Multiline Strings) feature standardized as part of Java SE 15. However, this is not necessary and you can write it the way you posted it in your question.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0
(?i)(\\s)+like(\\s)*('%\")(.*?)(\"%')

.*? = zero or more times

the Hutt
  • 16,980
  • 2
  • 14
  • 44
0

To get the desired matches, you can also use a match without any capture groups and match zero or more chars after the '%"

(?i)(?<!\S)like\s*'%".*?"%'

Regex demo | Java demo

In Java

String regex = "(?i)(?<!\\S)like\\s*'%\".*?\"%'";
The fourth bird
  • 154,723
  • 16
  • 55
  • 70