3

I want to capture date and some other information from a string in java using regex.

I group my pattern as follows,

"( ( date_variation_1 | date_variation_2) (some_other_info) ) "

And now, I want to extract the matched string like this,
group0 - the whole match
group1 - date
group2 - some other info

My problem is that I need to use parentheses internally in date_variation_1, date_variation_2 and some_other_info and those parentheses will also be treated as group dividers.

Is there any easy work around for this, i.e. define some other special charter as outer group divider, instead of parentheses?

date_variation_1:

"(((?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday|Tues|Thur|Thurs|Sun|Mon|Tue|Wed|Thu|Fri|Sat))" // Day Of Week 1
                        + "(\\s+)"  // White Space 1
                        + "((?:[0]?[1-9]|[1][012])[-:\\/.](?:(?:[0-2]?\\d{1})|(?:[3][01]{1}))[-:\\/.](?:(?:\\d{1}\\d{1})))(?![\\d])" // MMDDYY 1
                        + "(\\s+)" // White Space 2
                        + "((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|AM|pm|PM))?))"; // HourMinuteSec 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
carloscloud
  • 351
  • 1
  • 3
  • 14
  • Also, in Java you don't need the outer parentheses to match everything. `group(0)` always gets the string that matches the whole expression. – Paŭlo Ebermann May 16 '11 at 11:00
  • @palo-ebermann I know that, but I want the outer parentheses to match specific parts of the regex. Even parentheses in those parts are considered as groups. Maybe you misunderstood my question a bit. – carloscloud May 16 '11 at 11:07

1 Answers1

7

You can use (?:yourGroupHere) to define non-capturing groups.

Jens
  • 25,229
  • 9
  • 75
  • 117