0
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
0

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for showing that string.replace supports RegEx (I didn't know that).
@0xCAFEBABE, sorry it doesn't, it's replaceFirst().
Thanx very much Qtax. It's working perfectly.Better if you can change ' to ". :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.