Last active
August 29, 2015 14:14
-
-
Save jasonyost/48d3ce19989f1ecf8a08 to your computer and use it in GitHub Desktop.
Regex to find mixed number in string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/^(\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