Regex to split years, months, and weeks - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Saturday, April 2, 2022

Regex to split years, months, and weeks

Given a string like this:

"7 years 11 months 3 weeks"

I need to parse it out so that in PHP, the preg_split function returns an array that contains:

0 => "7 years"
1 => "11 months"
2 => "3 weeks"

The age string can also contain zeros like "0 years 1 month 0 weeks".

The regex string I have right now only seems to split between years and months:

(?!\d+)(?:\s)(?!\D+)

and that is throwing off the calculations.

I'm not very familiar with regex and I can't figure out what I'm doing wrong.



source https://stackoverflow.com/questions/71710244/regex-to-split-years-months-and-weeks

No comments:

Post a Comment