-1

I am trying to replace the content "<mml:mo data-name="OPT_ID_1438"" to "<mml:mo" using regular expression . but can't achieve it . can anyone pls help me here . what iam doing wrong .

source code:

public static void main( String[] args )
{
    String strXmlTxt="<mml:mo data-name=\"OPT_ID_1438\" stretchy=\"true\">";
    strXmlTxt = strXmlTxt.replace("<mml:mo data-name=\"OPT_ID_1438\"","<mml:mo");
    String strXmlTxt1 = strXmlTxt.replace("<mml:mo data-name=\"(.*?)\">","<mml:mo>");
    System.out.println(""+strXmlTxt);
    System.out.println(""+strXmlTxt1);
}

Thanks in advance.

happy songs
  • 835
  • 8
  • 21
  • Couldnt you just use "split" function and take the first part? – Siva Rahul Jun 07 '22 at 07:51
  • 3
    Processing XML with regular expressions is a bad idea for many reasons. You should use a proper XML parser. If the input is real XML, rather than some locally-defined proprietary subset of XML, then your code needs to allow for attributes to be in any order, for whitespace to appear in lots of different places, for single quotes to be used rather than double quotes, for different namespace prefixes to be used. – Michael Kay Jun 07 '22 at 08:45

1 Answers1

-1

if you want use regex you should use 'replaceAll' or 'replaceFirst'

String strXmlTxt1 = strXmlTxt.replaceAll("<mml:mo data-name=\"(.*?)\">","<mml:mo>");