简体   繁体   中英

A Regex for Group Matching in Java

I have a row of data that is split by tabs. I want to do a counted matching: first three are in group 1, second three are in group 2, and last one in group 3.

0   0   1998-09-21 CD   O   O   B-Num

I'm not an expert in regex but I came up with this:

^(\\S*)[\\t,]*(\\S*)[\\t,]*(\\S*)[\\t,]*(\\S*)[\\t,]*(\\S*)[\\t,]*(\\S*)[\\t,]*(\\S*)[\\t,]*(.*)$

This will only split everything into seven pieces, which does not meet the requirement..anyone knows how to do tasks like this?

You can change the way you're capturing a little to this:

^(\S*[\t,]*\S*[\t,]*\S*)[\t,]*(\S*[\t,]*\S*[\t,]*\S*)[\t,]*(\S*)$
 ^---------------------^      ^---------------------^      ^---^

Also, you might want to change some of the quantifiers to + . having all * means that it will also attempt to match an empty string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM