I have some regex that I need to convert from mysql to java, but they don't work when passed to String.matches().
How do I convert a mysql regex to a java regex?
Is there any APIs (built-in or third-party) to do this?
It's really simple. Here's the difference:
String.matches()
needs to match the whole Stringregexp
only needs to match part of the stringTo convert a mysql regex into a java one, basically add ".*"
to each end of the regex, or otherwise convert it from a "partial" match to a full match.
Here's some examples to demonstrate:
"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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With