Jake
Jake

Reputation: 4660

java regex for 38k to 38000

How do I change 38k to 38000? or 43k to 48000 etc

I tried this

String s0=toClean.replaceAll("[0-9]k", "[0-9]000");

But its wrong. It changes 38k to 309000

Upvotes: 0

Views: 73

Answers (1)

nicael
nicael

Reputation: 19005

Not only one digit, but multiple (so add +). Also, you should capture that number and return it with the capture group 1 ($1) and check if there's nothing else after k (e.g. so as 38kabc wouldn't be valid)

String s0=toClean.replaceAll("([0-9]+)k\\b", "$1000");

Upvotes: 4

Related Questions