Reputation: 41
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.
I am using regex in java (?i)(\\s)+like(\\s)*('%\")(.+?)(\"%')
when i get the group it does not return correct result.
Please suggest.
Upvotes: 0
Views: 137
Reputation: 79085
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.
Upvotes: 1
Reputation: 163362
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*'%".*?"%'
In Java
String regex = "(?i)(?<!\\S)like\\s*'%\".*?\"%'";
Upvotes: 0