java - Regex - Finding numbers in a certain String after a char and then editing them? -
i have multiple files in directory, , files contain multiple string such as:
details: 0 5453 293
details: 1 4223 123
details: 2 8885 231
the problem since have multiple files, each file has different details different numbers.
how replace numbers after 0, 1, , 2?
i can't do:
.replace("details: 0 5453 293", "details: 0");
since not know numbers there after 0 other files.
you can use string.replaceall method regular expression this.
using lookaround assertion:
string s = "details: 2 8885 231"; s = s.replaceall("(?<=^details: \\d).*", ""); system.out.println(s); //=> "details: 2" or using capturing group:
string s = "details: 2 8885 231"; s = s.replaceall("^(details: \\d).*", "$1"); system.out.println(s); //=> "details: 2"
Comments
Post a Comment