Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java substring regex

Posted on 2010-11-18
3
Medium Priority
?
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

730 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