-1

I am taking an ISBN input by the user - may contain spaces and hyphens etc. - and trying to sanitise it to be only digits.

In Java and Javascript, I have used the following regex successfully

Java (isbn is a java.lang.String)

isbn = isbn.replaceAll("[^\\d]", "");

and, JavaScript

isbn = isbn.replace(/[^\d]/g, "");

However, some ISBNs can have an X as their checksum character. For example, 'The book of days' by Sara Reinke is '155404295X'

How can I change the regex to allow X as well as digits?

Update: [^\dX] worked in JavaScript, but [^\\dX] does not work in Java.

Update 2: PEBKAC! I was sanitising in two places - I updated one but not the other. [^\\dX] does work in Java as well.

Vihung
  • 12,947
  • 16
  • 64
  • 90

2 Answers2

0

Can you try [^0-9X] there? I think it will work in both Java and JavaScript. P.S. But \\d should work in Java too...

vdem
  • 82
  • 4
0

If you wanna follow your solution you can exclude with ?:(values) For your example it would be: [^\d?:(X)] tested in online java regex.

Naares
  • 1