Link to home
Start Free TrialLog in
Avatar of dportabella
dportabella

asked on

regular expressions in Java

I have the following function:

String transform(String input) {
    Pattern pattern = Pattern.compile("(\d+) (Jan|Feb|Mar) (\d\d\d\d)");
    Matcher matcher = pattern.matcher(input);
    return matcher.replaceAll("$3 $2 $1");
}

transform("3 Feb 1987") returns "1987 Feb 3"

That's ok.

How could I modify the transform function, so that
transform("3 Feb 1987") returns "1987 02 3"
?

I know that I could achieve it by adding extra java code,
so that it gets the month string, and then transform it into a number using a simple hashmap, for instance.

However, I would like to achieve this without adding any extra java code, just by modifying only the Pattern.compile and matcher.replaceAll instructions.
Is this possible? Maybe with another regular expression package?

I am thinking something such as the following:
    Pattern pattern = Pattern.compile("(\d+) (Jan=01|Feb=02|Mar=03) (\d\d\d\d)");
    Matcher matcher = pattern.matcher(input);
    return matcher.replaceAll("$3 $2 $1");

Do you know how to achieve this?



-----
ps: This is just an example for me to understand; I know that I could achieve it by using the Date formating and parsing functions of Java (so, without using regular expressions)
Avatar of kralikX
kralikX

Hi,

I don't think it is achievable modyfying only regexp and replacement pattern in your code. If you don't want to program specific logic for some concrete replacement (like Jan -> 01, etc. in your example), I guess the simplest you could do with is to match a set of regexps and patterns. Then you would pass separate regexp and replacement for each month.

KralikX.
Is there a reason you need to do this using regular expression? I think it might be easier to achieve this throug SimpleDateFormat:

DateFormat input = new SimpleDateFormat("d MMM yyyy", Locale.US);
DateFormat outputOne = new SimpleDateFormat("yyyy MMM d", Locale.US);
DateFormat outputTwo = new SimpleDateFormat("yyyy MM d", Locale.US);

Date date = input.parse("3 Feb 1987");
System.out.println(outputOne.format(date));
System.out.println(outputTwo.format(date));

Avatar of dportabella

ASKER

kralikX,
Maybe it is not possible using the regexp package from java.
What about other regular expression packages?
What about using a transducer package?

I really would like to have something like this:
    Pattern pattern = Pattern.compile("(\d+) (Jan=01|Feb=02|Mar=03) (\d\d\d\d)");
    Matcher matcher = pattern.matcher(input);
    return matcher.replaceAll("$3 $2 $1");


Bart_Cr,
As I said, that was just an example to learn about parsers/transducers in java.

Avatar of CEHJ
You can't do that with a regex alone - you need a lookup table. Of course the proper way to do it is shown by Bart_Cr
ASKER CERTIFIED SOLUTION
Avatar of kralikX
kralikX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I found something called Java Transducer Interface (JTI) but it is aimed to work with hardware sensors, etc.
Hello kralikX,

>.. it should be possible to extend regexp language in similar manner as you suggest
>(I'd choose syntax that would be easily parseable using regexp again) and preprocess it in your code.
Hey, I like that idea!
However, I would like to be sure that somebody else has not already done so, before going for it.
Of course, I would prefer that somebody had already done it, and to know about it :)


About a transducer package for java, I just found this project:
http://sourceforge.net/projects/esw/

However, the website about the SimpleTransduction example is not working:
http://esw.sourceforge.net/nodeandmetacollectionsexamples.html

> Of course, I would prefer that somebody had already done it, and to know about it :)
I understand that :-).

Thanks for the link to ESW, seems quite interesting.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)