Friday 15 August 2014

Java Regex Remove comma's between numbers from String -



Java Regex Remove comma's between numbers from String -

i trying parse string, remove commas between numbers. request read finish question , please answer.

let consider next string. :)

john loves cakes , orders them dialing "989,444 1234". johns credentials follows" "name":"john", "jr", "mobile":"945,234,1110"

assuming have above line of text in java string, now, remove comma's between numbers. replace next in same string: "945,234,1110" "9452341110" "945,234,1110" "9452341110" without making other changes string.

i iterate through loop, when ever comma found, check previous index , next index numbers , delete required comma. looks ugly. doesn't it?

if utilize regex "[0-9],[0-9]" loose 2 char, before , after comma.

i seeking efficient solution rather doing brute forcefulness "search , replace" on finish string. real time string length ~80k char. thanks.

public static void main(string args[]) throws ioexception { string regex = "(?<=[\\d])(,)(?=[\\d])"; pattern p = pattern.compile(regex); string str = "john loves cakes , orders them dialing \"989,444 1234\". johns credentials follows\" \"name\":\"john\", \"jr\", \"mobile\":\"945,234,1110\""; matcher m = p.matcher(str); str = m.replaceall(""); system.out.println(str); }

output

john loves cakes , orders them dialing "989444 1234". johns credentials follows" "name":"john", "jr", "mobile":"9452341110"

java

No comments:

Post a Comment