Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By Junky

Thread: java regular expressions

  1. #1
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default java regular expressions

    Hello,

    I am wondering if someone can help. I am new to Java and I need to write a program that takes a clob from a field in a database
    then parse the clob and populate the data into a new table

    To do this - so far I have exported the CLOB to an ixf file.
    I have created a java program that reads in the IXF file.
    The CLOB contains a dump of XML and NON XML text. The NON XML stuff starts before the root xml element.
    I am stuck at the part of the regular expression.


    I want the expression to bring back results based on the pattern for the tag elements
    example
    String pattern = "(?i)(<Id.*?>)(.+?)(</Id>)";

    Java Code:
    BufferedReader br = null;
    		 
    		
    		// Extract the text between the two elements
    	//	String pattern = "(?i)(<Id.*?>)(.+?)(</Id>)";
    		String pattern = "(?i)(<Status.*?>)(.+?)(</Status>)";
    	/*	String pattern = "(?i)(<date.*?>)(.+?)(</date)";
    		String pattern = "(?i)(<time.*?>)(.+?)(</time>)";
    		String pattern = "(?i)(<offset.*?>)(.+?)(</offset>)";
    		String pattern = "(?i)(<Diagnostic.*?>)(.+?)(</Diagnostic>)";
    		String pattern = "(?i)(<Message-Text.*?>)(.+?)(</tMessage-Text>)";
    		String pattern = "(?i)(<CurrentMessage-Name.*?>)(.+?)(</CurrentMessage-Name>)";
    		
    	*/	
    	
    		
    		
    		
    		try {
     
    			String sCurrentLine;
    			 
     
    			br = new BufferedReader(new FileReader("C:/COMMENTTEXT.ixf"));
    			
    			while ((sCurrentLine = br.readLine()) != null) {
    				Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
    				Matcher matcher = p.matcher(sCurrentLine);
    				 
    				boolean didMatch = matcher.matches();
    				 String [] items = p.split(sCurrentLine);
                     for(String s : items) {
                         System.out.println(s);
                     }
    	/*			Matcher matcher = p.matcher(br.readLine());  
    				
    				
    	            while(matcher.find()){    
    	//            	String [] group = matcher.group().split("\\s");
    	                
    	                 }
    	//                System.out.println("First Name "+group[1]);    
    	 //               System.out.println("Last Name " +group[2]);    
    	            }*/
    	//			System.out.println(pattern);
    	//			String[] array1 = sCurrentLine.split("<");
    			    // check to make sure you have valid data
    	//		    String[] array2 = array1[1].split("<,/>");
    	//		    System.out.println(Arrays.toString(array1));
    	//		    System.out.println(Arrays.deepToString(array1));;
    	//		    System.out.println(Arrays.deepToString(array2));;
    			    
    	//			System.out.println(sCurrentLine);
    			}
     
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (br != null)br.close();
    			}
    This code is bringing back everything except the pattern

    Can anyone help ?
    Also - would my next steps be to put these into multiple arrays and then apply getters and setters and try and populate the database ?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: java regular expressions

    Why not use an XML parser?
    DarrylBurke and gimbal2 like this.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: java regular expressions

    Quote Originally Posted by Junky View Post
    Why not use an XML parser?
    How DARE you suggest that there is a better solution than using a regex! Regexes solve ALL problems, including world hunger.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    alexandra12 is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: java regular expressions

    Can i use xml parser when non xml text is contained outside of the start of the root xml ? Also - when I converted the ixf file to an xml file and tried to read it in it complained that it was not well formed and would not parse it.

    Sample screenshot attached java regular expressions-xml.jpg

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: java regular expressions

    Generally no, XML parsers are very strict. With good reason, XML is intended to be used for information exchange after all; allowing garbage makes no sense. You might try stripping off everything "around" the XML data, so for example everything before the <?xml part.

    EDIT: of course now that you put the thread into context it makes more sense that you were attempting to use a regex, but that doesn't change the fact that regexes are notoriously bad for trying to parse XML data.
    Last edited by gimbal2; 08-28-2013 at 01:37 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Java and regular expressions
    By kosmos890 in forum New To Java
    Replies: 3
    Last Post: 01-18-2013, 03:57 PM
  2. regular expressions in java
    By chkontog in forum New To Java
    Replies: 2
    Last Post: 11-07-2012, 03:53 PM
  3. Java Regular Expressions: Comma Seperated List
    By sgtblitz in forum New To Java
    Replies: 3
    Last Post: 04-18-2011, 09:17 PM
  4. How to create regular expressions in Java
    By maz09 in forum New To Java
    Replies: 12
    Last Post: 04-02-2010, 05:13 PM
  5. Regular Expressions in java
    By blue404 in forum Advanced Java
    Replies: 2
    Last Post: 09-26-2008, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •