[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

regular expressions in Java

Posted on 2007-08-08
10
Medium Priority
?
407 Views
Last Modified: 2013-11-05
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)
0
Comment
Question by:dportabella
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 2

Expert Comment

by:kralikX
ID: 19655980
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.
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 19661793
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));

0
 

Author Comment

by:dportabella
ID: 19661919
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.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 19663133
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
0
 
LVL 2

Accepted Solution

by:
kralikX earned 1000 total points
ID: 19668224
Sorry, never heard about a transducer package in Java. Do you have any link to it?

However if you don't want to hardcode the transformation directly into code (like Bart_Cr has shown), 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.

Parser IMHO won't help you, you'd still have to write code for the transformation.
0
 
LVL 2

Expert Comment

by:kralikX
ID: 19668228
I found something called Java Transducer Interface (JTI) but it is aimed to work with hardware sensors, etc.
0
 

Author Comment

by:dportabella
ID: 19668369
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

0
 
LVL 2

Expert Comment

by:kralikX
ID: 19668521
> 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.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 19668560
Of course you can simply make your own parser to do this

https://javacc.dev.java.net/
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19986730
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 10 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question