Solved

java substring regex

Posted on 2010-11-18
3
674 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 500 total points
Comment Utility
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
Comment Utility
Can you please explain the regular expression that is being used in the Pattern? Thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
What bit don't you understand?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now