admin管理员组文章数量:1122846
I was looking for a way to validate US ZIP codes and got an answer here. I tested it using regex101 site and confirmed.
However, when I use the same expression in an Oracle stored procedure and pass the exact same input, it fails to recognize it.
The regular expression checks if input is 5 or 9 digits, or 5 digits + dash + 4 digits, and does not start with 3 or more zeros.
^(?!000)\d{5}(-\d{4})?$
Using input 08731-5107, it passes regex test, but fails in this Oracle query:
IF REGEXP_LIKE(LVPOSTCODE, '^(?!000)\d{5,9}$') OR REGEXP_LIKE(LVPOSTCODE, '^(?!000)\d{5}(-\d{4})?$') THEN ...
where LVPOSTCODE is 08731-5107.
Can someone tell me if I am doing something wrong for the above to fail?
I was looking for a way to validate US ZIP codes and got an answer here. I tested it using regex101 site and confirmed.
However, when I use the same expression in an Oracle stored procedure and pass the exact same input, it fails to recognize it.
The regular expression checks if input is 5 or 9 digits, or 5 digits + dash + 4 digits, and does not start with 3 or more zeros.
^(?!000)\d{5}(-\d{4})?$
Using input 08731-5107, it passes regex test, but fails in this Oracle query:
IF REGEXP_LIKE(LVPOSTCODE, '^(?!000)\d{5,9}$') OR REGEXP_LIKE(LVPOSTCODE, '^(?!000)\d{5}(-\d{4})?$') THEN ...
where LVPOSTCODE is 08731-5107.
Can someone tell me if I am doing something wrong for the above to fail?
Share Improve this question asked Nov 21, 2024 at 17:15 NoBullManNoBullMan 2,1766 gold badges50 silver badges106 bronze badges1 Answer
Reset to default 0Oracle database supports only POSIX standard of regex.Hence you have to convert your regex to POSIX standard OR With inputs from this answer
IF you have java installed in your database you can use the below
CREATE AND COMPILE JAVA SOURCE NAMED RegexpMatch AS
import java.util.regex.Pattern;
public class RegexpMatch {
public static int match(
final String value,
final String regex
){
final Pattern pattern = Pattern.compile(regex);
return pattern.matcher(value).matches() ? 1 : 0;
}
}
Then wrap it in an SQL function
CREATE FUNCTION regexp_java_match(value IN VARCHAR2, regex IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA NAME 'RegexpMatch.match( java.lang.String, java.lang.String ) return int';
/
And use like the below
IF regexp_java_match(LVPOSTCODE, '^(?!000)\d{5,9}$')=1
then
--do processing
end if;
本文标签: regexp likeOracle REGEXPLIKE fails to recognize valid inputStack Overflow
版权声明:本文标题:regexp like - Oracle REGEXP_LIKE fails to recognize valid input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308603a1933718.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论