Java – captures groups in regular expressions only in outer parentheses

I want to use regular expressions to capture dates and other information from strings in Java

I grouped my patterns as follows,

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

Now, I want to extract the matching string like this, group0 - the whole game, group1 - the date, group2 - some other information

My problem is that I need to be on date_ variation_ 1,date_ variation_ 2 and some_ other_ Info uses parentheses internally, which will also be treated as group separators

Is there any simple solution to define some other special rules as external group separators 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

Solution

You can use (?: yourgrouphere) to define non capture groups

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>