Skip to content

Instantly share code, notes, and snippets.

@jasonyost
Last active August 29, 2015 14:14
Show Gist options
  • Save jasonyost/48d3ce19989f1ecf8a08 to your computer and use it in GitHub Desktop.
Save jasonyost/48d3ce19989f1ecf8a08 to your computer and use it in GitHub Desktop.
Regex to find mixed number in string
/^(\d++(?! *\/))? *-? *((?:(\d+) *\/ *(\d+)))?(.*)$/
# "1 1/2 parts".scan(/^(\d++(?! *\/))? *-? *((?:(\d+) *\/ *(\d+)))?(.*)$/) => [["1", "1/2", "1", "2", " parts"]]
# "25ml".scan(/^(\d++(?! *\/))? *-? *((?:(\d+) *\/ *(\d+)))?(.*)$/) => [["25", nil, nil, nil, "ml"]]
# "1 ounce".scan(/^(\d++(?! *\/))? *-? *((?:(\d+) *\/ *(\d+)))?(.*)$/) => => [["1", nil, nil, nil, "ounce"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment