Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java substring regex

Posted on 2010-11-18
3
Medium Priority
?
742 Views
Last Modified: 2013-11-23
New York Housing May 11 F
New York Housing May12 Jun12 F
New York Housing May 12 Aug 12 O
NYMEX RBOB Gasoline Options Jan10 O

The last digit can be either F or O. The spaces can be present or not in between Month and Year and SecurityType (F or O)

Can you please give me the method to get the substring such that the method should return
201105 for the first one
201205201206 for the second one
201205201208 for the third one.
201001 for the fourth one.

in the fastest efficient way possible?

I need this in Java
0
Comment
Question by:srikanthrad
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 34167547
Put those strings into an array called 'DATES' then try:

final Pattern DATE_PAT = Pattern.compile("([MJASONDF][a-z]{2})\\s*?(\\d{2})");
	for (String s : DATES) {
	    Matcher m = DATE_PAT.matcher(s);
	    while(m.find()) {
		System.out.println(m.group(1) + " " + m.group(2));
	    }
	    System.out.println();	
	}

Open in new window

0
 

Author Closing Comment

by:srikanthrad
ID: 34168614
Can you please explain the regular expression that is being used in the Pattern? Thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34172129
What bit don't you understand?
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
A solution for Fortify Path Manipulation.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

972 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