2

I have a service period mentioned as

MAY 01-15

and I need to extract start and end date as

group 1 : MAY 01
group 2 : MAY 15

is it possible???

2 Answers2

0

It is possible.

This Expression

@(Jan|Feb|May) (\d{1,2})-(\d{1,2})@is

gives you the following Array

Array
(
    [0] => May 01-15
    [1] => May
    [2] => 01
    [3] => 15
)

You should expand the first parenthesis with all the months you need/want.

Dennis Christian
  • 166
  • 2
  • 14
0

How about using Java - String replaceAll() Method? Assuming your input is like sample.

String str = "MAY 01-15";

String start = str.replaceAll("-\\d+$", "");
System.out.println(start);

MAY 01

String end = str.replaceAll("\\b\\d+-", "");
System.out.println(end);

MAY 15

See test at ideone.com; Regex test at regexplanet (click on "Java")

Also see SO regex faq


If the sample is only a fragment, first extract the relevant part:

String full_str = "foo bar MAY 01-15 Lorem ipsum dolor sit amet, consetetur, adipisci velit";
String str = full_str.replaceAll("(?s)^.*?(\\b[A-Z]{3,}\\s+\\d{2}-\\d{2})(?!-).*", "$1");

Explanation at regex101 (top right)

Community
  • 1
  • 1
Jonny 5
  • 11,051
  • 2
  • 20
  • 42