Convert mysql regex to java regex (and / or vice versa)

I have some regex that I need to convert from mysql to java, but they don't work when passing to String.matches ().

How to convert mysql regex to java regex?
Are there any APIs (built-in or third-party) for this?

+4
source share
1 answer

It is really easy. Here is the difference:

To convert the mysql regular expression to java, basically add ".*" To each end of the regular expression or otherwise convert it from a β€œpartial” match to a full match.

Here are some examples to demonstrate:

Java

 "xyz".matches("y"); // false - only matches part of the input "xyz".matches(".*y.*"); // true "xyz".matches("[xyz]"); // false - only matches one char, but String is 3 chars "xyz".matches("[xyz]+"); // true 

MySQL

 select 'xyz' regexp 'y'; -- 1 (ie true) select 'xyz' regexp '.*y.*'; -- 1 (ie true) select 'xyz' regexp '[xyz]'; -- 1 (ie true) select 'xyz' regexp '[xyz]+'; -- 1 (ie true) 
+5
source

Source: https://habr.com/ru/post/1389590/


All Articles