简体   繁体   中英

Splitting string by using regex

I have a long string in format:

"DD.MM.YYYY comments for this date ; DD.MM.YYYY comments for another date ; etc".

I want to split the string for array (date + description). I tried to split by using this pattern, but it matches the entire string instead of splitting.

(\d{2}[.]\d{2}[.]\d{4})[^(\1)]*

I can't split by ; divider, because text can possibly include it.

Try:

(\d{2}[.]\d{2}[.]\d{4})\D*

This will match a date followed by any non-digit character.

If the description can contain digits, then try:

((\d{2}[.]\d{2}[.]\d{4}).*?(; (?=\d))|(\d{2}[.]\d{2}[.]\d{4}).*$)

This will match a date followed by anything until meeting a semi-colon followed by a space and a digit or a date followed by anything until the end of the string.

If you want to use the date as a divider (instead of the semi-colon followed by a space and a digit), then try this:

((\d{2}[.]\d{2}[.]\d{4}).*?((?=(\d{2}[.]\d{2}[.]\d{4})))|(\d{2}[.]\d{2}[.]\d{4}).*$)

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