简体   繁体   English

将mysql regex转换为java regex(反之亦然)

[英]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 passed to String.matches(). 我有一些正则表达式,我需要将其从mysql转换为java,但是当传递给String.matches()时它们不起作用。

How do I convert a mysql regex to a java regex? 如何将mysql regex转换为java regex?
Is there any APIs (built-in or third-party) to do this? 是否有任何API(内置或第三方)可以做到这一点?

It's really simple. 真的很简单。 Here's the difference: 区别在于:

To 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. 要将mysql正则表达式转换为Java正则表达式,基本上在正则表达式的每一端都添加".*" ,或者将其从“部分”匹配转换为完全匹配。

Here's 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 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM