Java 1.3 ORO Regex Problem

keks

keks

nicht 1337 genug
Hallo Forum,

ich habe eine software die Leider unter Java 1.3_10 laufen muss.
Als regex "framework" nutzte ich jakarta ORO.

Mein Problem ist, ich bekomme es nicht hin ORO zu überreden auf einen String ala "01.12.2009" zu matchen...
ein regex von "([0-9]{1,2})" matcht ohne Probleme auf "09" (also das letzte vorkommen 2er Zahlen).
Ich glaube mir raucht der Schädel zu sehr um das alleine zu lösen :headup:

Code:
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.oro.text.regex.*;

public class RegexTester {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Date date = new Date();
		int groups = 0;
		PatternMatcher matcher;
		PatternCompiler compiler;
		Pattern pattern = null;
		PatternMatcherInput input;
		MatchResult result;

		compiler = new Perl5Compiler();
	    matcher  = new Perl5Matcher();
		
	    String strDate = "01.02.2009";
		
		/* eg 12.01.2009 or 1.2.09 */
		//String strPatrn = "([0-9]{1,2})\\.([0-9]{1,2})\\.([0-9]{4})";
	    //String strPatrn = "([0-9]{1,2}).([0-9]{1,2}).([0-9]{2,4})";
	    String strPatrn = "([0-9]{1,2}).+([0-9]{1,2})";
		try {
			pattern = compiler.compile(strPatrn);
		} catch (MalformedPatternException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		input   = new PatternMatcherInput(strDate);
		
		System.out.println("Using str: " + strDate);
		System.out.println("Using pattern: " + strPatrn);
		
		while(matcher.contains(input, pattern)) {
			result = matcher.getMatch();  
			groups = result.groups();
			
//			date = new Date(
//					Integer.parseInt(result.group(0)),
//					Integer.parseInt(result.group(1)),
//					Integer.parseInt(result.group(2))		
//			   	   );
			System.out.println(result.group(0));
			System.out.println(result.group(1));
			System.out.println(result.group(2));
		}
	}

}


Liebe Grüße

Keks
 
So ich habe es mir selbst (gemacht:devil:) gelößt!

Code:
import java.util.Date;

import org.apache.oro.text.regex.*;

public class RegexTester {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Date date = new Date();
		int groups = 0;
		PatternMatcher matcher;
		PatternCompiler compiler;
		Pattern pattern = null;
		PatternMatcherInput input;
		MatchResult result;

            compiler = new Perl5Compiler();
	    matcher  = new Perl5Matcher();
		
	    String strDate = "07.09.2009";
		
		/* eg 12.01.2009 or 1.2.09 */
	      //String strPatrn = "([0-9]{1,2})\\.([0-9]{1,2})\\.([0-9]{4})";
	      //String strPatrn = "([0-9]{1,2}).([0-9]{1,2}).([0-9]{2,4})";
	        String strPatrn = "([0-9]{1,2})\\.([0-9]{1,2})\\.([0-9]{2,4})";
		try {
			pattern = compiler.compile(strPatrn);
		} catch (MalformedPatternException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		input   = new PatternMatcherInput(strDate);
		
		System.out.println("Using str: " + strDate);
		System.out.println("Using pattern: " + strPatrn);
		
		while(matcher.contains(input, pattern)) {
			result = matcher.getMatch();  
			groups = result.groups();
			

			date = new Date(
					Integer.parseInt(result.group(3))-1900, // Year - 1900
					Integer.parseInt(result.group(2))-1, // Month 0-11 -> also decrement by 1!
					Integer.parseInt(result.group(1))	 // Day of Month 1-31	
			   	   );
			System.out.println("result.group(0): " + result.group(0));
			System.out.println("result.group(1): " + result.group(1));
			System.out.println("result.group(2): " + result.group(2));
			System.out.println("result.group(3): " + result.group(3));
		}
		System.out.println("New generated Date: \"" + date.toString()+ "\"");
	}

}
Ausgabe ist:

Code:
Using str: 07.09.2009
Using pattern: ([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{2,4})
result.group(0): 07.09.2009
result.group(1): 07
result.group(2): 09
result.group(3): 2009
New generated Date: "Mon Sep 07 00:00:00 CEST 2009"

Erkenntnis: group0 ist das ganze gespeicherte Ergebniss... UUUND ordentlich Lesen
Grüße
Keks!
 

Ähnliche Themen

"non blocking console input" wieder rückgängig machen?

Input == String => false

Displayport + externer Monitor zeigt bei startx nichts erst bei DVI

NagiosGrapher 1.7.1 funktioniert nicht

dovecot und postfix Konfiguration Problem

Zurück
Oben