简体   繁体   中英

Regex with case insensitive prefix

I am trying to create a regex that's case insensitive prefix. I have the following but I am not sure whether that's correct or not:

String value1 = "97 ebt ue i ua so sufi iqc k";
String pattern1 = "(?=(\\b(?i:.*s.*)))(?=(\\b(?i:.*q.*)))";

In my example I am trying to find a match for pattern1 in value1 . As far as I understand:

  1. \\\\b matches any word in the string
  2. :.* and .* makes case insensitive

This works with some cases but with others fails. I can't figure out the issue.

UPDATE:

This case here returns true but it should return false. The thing is there should be any word that ends with s and another one that ends with q so since nothing ends with any of them so it should fail.

The \\\\b construct is just a word boundary, it does not match a word.

(?i) can be used n the beginning just once, and the whole pattern will be case insensitive.

The thing is there should be any word that ends with s and another one that ends with q so since nothing ends with any of them so it should fail

Use

(?Ui)^(?=.*s\\b)(?=.*q\\b)

Here, we have two anchored look-aheads requiring 2 words ending with q and s inside the input string (case-insensitive).

Here is the code demo :

String value1 = "97 ebts ue i ua so sufi iqc k";
String pattern1 = "(?Ui)^(?=.*s\\b)(?=.*q\\b)";
Pattern ptrn = Pattern.compile(pattern1);
Matcher matcher = ptrn.matcher(value1);
if (matcher.find())
    System.out.println("true");
else
    System.out.println("false");

If you wonder what ?U means it is just "fixing" the \\b word boundary within Unicode strings. If you do not use any letters other than Latin, you can remove the U .

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