简体   繁体   中英

Regex - I need to replace all digits but specific 3 digits numbers

I want to replace all digits on a log with "X", But i want to keep the HTTP status codes for info.

I've been trying different approaches. None of them worked.

str.replaceAll("(?!5\d\d|4\d\d)\d{3}", "X") //Didn't worked

// Error trying to POST to /shipments/X5X5X/select?caller.id=XX592: 500 INTERNAL_SERVER_ERROR

I need it to be:

From:

Error trying to POST to /shipments/28056415973/select?caller.id=116089592: 500 INTERNAL_SERVER_ERROR

To:

Error trying to POST to /shipments/XXXX/select?caller.id=XXXX: 500 INTERNAL_SERVER_ERROR

If the numbers you want to select are never followed by a space and an uppercase char AZ you could use a negative lookahead:

\b[0-9]+\b(?! [A-Z])

Explanation

  • \\b[0-9]+\\b Match 1+ digits between word boundaries
  • (?! Negative lookahead, assert what is on the right is not
    • [AZ] Match a space and uppercase char AZ
  • ) Close lookahead

Regex demo | Java demo

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