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???
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???
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.
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")
$
is the end anchor\d
is a short for [0-9]
+
quantifier for 1 or more\b
matches a word boundaryAlso 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)