简体   繁体   English

Java 正则表达式数据库和特殊字符

[英]Java regex db and special character

I inherit from a java code not documented.我从未记录的 java 代码继承。 It seems that I have a database that contains code like this 050.09.89 and in java backend code a regex test.似乎我有一个数据库,其中包含这样的代码 050.09.89 和 java 后端代码中的正则表达式测试。

With an object corresponding to a database table getCode.matches() Now I must change the regex to match two kind of code 050.09.89 or 050/09/89 but I have 500 internal error when it's 050/09/89 and I think it's due to the database that contains only 050.09.89 type of code.使用对应于数据库表 getCode.matches() 的 object 现在我必须更改正则表达式以匹配两种代码 050.09.89 或 050/09/89 但是当它是 050/09/89 时我有 500 个内部错误,我认为这是由于数据库只包含 050.09.89 类型的代码。

Is the problem come from the regex or the database context?问题来自正则表达式还是数据库上下文? and how I can resolve the problem without changing db?以及如何在不更改数据库的情况下解决问题? Best Regards此致

private void checkNotifyAllowed(Deployment deployment) {

if (!deployment.getCode().matches(regex:"[0-9]{3}\\/[0-9]{2}\\/[0-9]{2}$|[0-9]{3}\\.[0-9]{2}\\.[0-9]{2}$")){

throw new BusinessException("Invalid billing code, should be nnn.nn.nn or nnn/nn/nn");
}}

Now I must change the regex to match two kind of code 050.09.89 or 050/09/89 but I have 500 internal error when it's 050/09/89 and I think it's due to the database that contains only 050.09.89 type of code.现在我必须更改正则表达式以匹配两种代码 050.09.89 或 050/09/89 但是当它是 050/09/89 时我有 500 个内部错误,我认为这是由于数据库只包含 050.09.89 类型代码。

That regex in the code block you shared is actually already constructed so that it accepts the formats XXX/XX/XX as well as YYY.YY.YY - the pipe character |您共享的代码块中的正则表达式实际上已经构建,因此它接受格式XXX/XX/XX以及YYY.YY.YY - pipe 字符| in the middle separates the two possibilities from each other.中间将两种可能性分开。 The \\ in there is to escape special characters.那里的\\用于转义特殊字符。 I don't think that the / needs to be escaped at all, unlike .我认为/根本不需要转义,不像. which is a special character in regular expressions and has to be escaped so it matches a literal dot character instead of any single character.这是正则表达式中的特殊字符,必须进行转义,以便匹配文字点字符而不是任何单个字符。 Like Wiktor suggested: replace \\/ with / .就像 Wiktor 建议的那样:将\\/替换为/

A 500 internal error could point to some other trouble as well, though, as / is kind of a special character as well.但是,500 内部错误也可能指向其他问题,因为/也是一种特殊字符。 Maybe it is confusing the web server because eg input parameters via a GET request with / characters are being treated like subdirectories access requests.也许它混淆了 web 服务器,因为例如通过带有/字符的 GET 请求的输入参数被视为子目录访问请求。 If it is possible for you, inspect your web server logs about this.如果可能,请检查有关此问题的 web 服务器日志。

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

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