Garrett Massey
Garrett Massey

Reputation: 109

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.

Upvotes: 2

Views: 182

Answers (2)

MikeM
MikeM

Reputation: 13631

The simplest solution is

$age = '7 years 11 months 3 weeks';

$result = preg_split('/ (?=\d)/', $age);

print_r($result);

Your regex is exactly equivalent to \s(?!\D) which should also work anyway, though if there was a space at the end of the string you would get an unwanted empty string result.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626903

You can use

preg_match_all('~\d+\s+\w+~', $text, $matches)

See the regex demo.

Note that preg_match_all extracts all occurrences of pattern matches.

Details:

  • \d+ - one or more digits
  • \s+ - one or more whitespaces
  • \w+ - one or more letters/digits/underscores.

Other regex variations:

  • \d+\s+\S+ - the \S+ matches one or more non-whitespace chars, so it will match words with hyphens and any other special chars
  • \d*\.?\d+\s+\S+ - \d*\.?\d+ will match both integer and float values.

NOTE: To get the number and word as submatches capture them:

preg_match_all('~(\d+)\s+(\w+)~', $text, $matches)

Upvotes: 1

Related Questions