2 Replies Latest reply: Oct 3, 2008 6:19 AM by 807589 RSS

    Regular Expression issue.

    807589
      I am reading one xml file in string like
      String srcStr = "<?xml version="1.0" encoding="utf-16"?><body><myTag>Data</myTag>......Multiple tags........</body>";
      Now i want to remove header part from string .
           Matcher matchHeaderUnit = Pattern.compile("<body>" ).matcher(srcStr);
              if (matchHeaderUnit.find()){
                 header = srcStr.substring(0,matchHeaderUnit.end());
                System.out.println(header);
                Matcher matchXMLStart = Pattern.compile(header.trim()).matcher(srcStr);
                   // Fail at here not able to find xml tag
                if ( matchXMLStart.find()){
      // it is not able to find start of xml line ..............
                     srcStr = srcStr.replaceFirst(matchXMLStart.group(),"");
                }
                System.out.println(srcStr);
              }
      Please suggest solution on this

      Edited by: AmitChalwade123456 on Oct 3, 2008 6:46 AM

      Edited by: AmitChalwade123456 on Oct 3, 2008 6:49 AM

      Edited by: AmitChalwade123456 on Oct 3, 2008 6:49 AM
        • 1. Re: Regular Expression issue.
          807589
          AmitChalwade123456 wrote:
          Please suggest solution on this
          My solution - don't use a regex approach. Use the approach I suggest in http://forums.sun.com/thread.jspa?messageID=4298502 reply #8.
          • 2. Re: Regular Expression issue.
            807589

            Hi
            The problem is due to ? in XML header line....Refere below code....
            I meanyou should replace ? with \\? while putting it in Pattern.compile("xmlHeader").

            Thanks
            String srcStr = "<?xml version=\"1.0\" encoding=\"utf-16\"?><body><myTag>Data</myTag>......Multiple tags........</body>";
            
            
            Matcher matchHeaderUnit = Pattern.compile("<body>" ).matcher(srcStr);
            
            
            *if* (matchHeaderUnit.find()){
            
            
            String header = srcStr.substring(0,matchHeaderUnit.start());
            
            
            System.out.println(header);
            
            
            Matcher matchXMLStart = Pattern.compile("\\?xml?").matcher("<?xml version=\"1.0\" encoding=\"utf-16\"?>");
            
            
            // Fail at here not able to find xml tag
            
            
            matchXMLStart.find();
            
            
            // it is not able to find start of xml line ..............
            
            
            System.out.println("Group=="+matchXMLStart.start());
            
            
            // srcStr = srcStr.replaceFirst(matchXMLStart.group(),"");
            
            
            System.out.println(srcStr);
            
            
            }