Solved

java substring regex

Posted on 2010-11-18
3
690 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 84
HTTPSessionBindingListernter not configured in deployment descriptor 2 53
servlet filter example 37 63
How to convert from xls to xlsx using java 7 39
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…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

777 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