String str = "abcde123_92qwq_1a_02x_1e";
I want to replace the first string part between the first two underscores (92qwq) with 0 (zero). How can I do this with regex?
For example:
"abcde123_92qwq_1a_02x_1e" becomes "abcde123_0_1a_02x_1e"
"abcde123_sdet4_1a_02x_1e" becomes "abcde123_0_1a_02x_1e"
I'm a newbie to the regex and I have tried a few. But I'm in a kind of bit urgent situation.
nhahtdh
56.9k15 gold badges131 silver badges164 bronze badges
asked Jun 21, 2012 at 8:25
namalfernandolk
9,16415 gold badges70 silver badges119 bronze badges
1 Answer 1
You can use something like:
str = str.replaceFirst("_[^_]+_", "_o_");
namalfernandolk
9,16415 gold badges70 silver badges119 bronze badges
answered Jun 21, 2012 at 8:28
Qtax
34k9 gold badges92 silver badges127 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
0xCAFEBABE
+1 for showing that string.replace supports RegEx (I didn't know that).
Qtax
@0xCAFEBABE, sorry it doesn't, it's
replaceFirst().namalfernandolk
Thanx very much Qtax. It's working perfectly.Better if you can change ' to ". :)
lang-java