复杂的php正则表达式的意思〜([0-9 /] +)(cup | tablespoon)s? ([-A-Z] +)~i

Can someone break this down for me? It's going into a preg_match_all() function.

~([0-9 /]+) (cup|tablespoon)s? ([-A-Z ]+)~i

It's most likely from a recipe. The regex will match when the input:

  • starts with any number, space, or slash, any number of times
  • followed by "cup", "cups", "tablespoon", or "tablespoons"
  • followed by any capital letter, dash, or space
  • case insensitive

Works with:

3 cups water
1 tablespoon TOOTHPASTE
1/3 cups Apple Juice
1/3/5 6 cups ASDF-TEST TEST

Side note: You should Google it next time.