jmann jmann - 3 months ago 7
Java Question

regex for '(number)'

I need to replace all the

'
s around numbers, to nothing.. for example:

'1' to 1

'100' to 100


which is the optimal way to do this? is there a regex to do this so I can use it in the
replace()
function of the
String
class?

Answer

You can use replaceAll method with regex support:

str = str.replaceAll("'(\\d+)'", "$1");

(\\d+) will match and group digits surrounded by single quotes on either side and then we use $1 in replacement which is the back-reference to captured value in regex.

Comments