Java String replaceAll with conditions

Posted by user1483570 on Stack Overflow See other posts from Stack Overflow or by user1483570
Published on 2012-12-07T23:02:12Z Indexed on 2012/12/07 23:03 UTC
Read the original article Hit count: 190

Filed under:
|
|
|

I am not good in regular expressions and I need help in replacing the string.

String str = "Name_XYZ_";
       str = "XYZ_NAME_";

So how can I replace "Name_" or "_NAME_" from above two strings with empty string? The conditions are "Name" can be in any case and it can be at index 0 or at any index but preceded by "_".

So far I tried,

String replacedString = str.replaceAll("(?i)Name_", ""); // This is not correct.

This is not the homework. I am working on XML file that needs such kind of processing. Please help. Thank you.

© Stack Overflow or respective owner

Related posts about java

Related posts about regex