简体   繁体   中英

Java regex db and special character

I inherit from a java code not documented. It seems that I have a database that contains code like this 050.09.89 and in java backend code a regex test.

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.

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.

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 | 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 / .

A 500 internal error could point to some other trouble as well, though, as / is kind of a special character as well. Maybe it is confusing the web server because eg input parameters via a GET request with / characters are being treated like subdirectories access requests. If it is possible for you, inspect your web server logs about this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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