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.

0

1 Answer 1

3

You can use something like:

str = str.replaceFirst("_[^_]+_", "_o_");
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

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.