Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java case insensitive regular expression

Tags:

java

regex

case

I just got this:

System.out.println("КОНТАКТЫ".matches("(?i).*контакт.*"));

and it always says "false".

I tried:

System.out.println("КОНТАКТЫ".matches("(?i:.*контакт.*)"));

and:

System.out.println("КОНТАКТЫ".matches("(?i:).*контакт.*"));

and:

System.out.println("КОНТАКТЫ".matches("(?i)^.*контакт.*$"));

and many others variations but I always have "false".

How to perform case insensitive search in java correctly so I have "true" in this regexp search?

What do I do wrong?

like image 642
SuperYegorius Avatar asked Mar 25 '26 19:03

SuperYegorius


1 Answers

What you need is to turn unicode case flag on:

System.out.println("КОНТАКТЫ".matches("(?iu).*контакт.*"));

More here

like image 134
Admit Avatar answered Mar 28 '26 07:03

Admit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!