need help with regex pattern for string containing vulgar fraction characters

Working on a web scraping script in python to get recipe data for an application and trying to do some light processing on it to clean it up before importing it. A majority of it has went really well but I can't figure out a regex pattern to match some of the ingredient lists and hoping someone here can help me. I think its because of the vulgar fraction symbols but I am not sure. My goal is to split the quantities from the descriptions.

​

A possible ingredients list is below and under each is where I would want to split them:

  • 1 (28 ounce) can crushed tomatoes
    • 1
    • (28 ounce) can crushed tomatoes
  • 2 tablespoons white sugar
    • 2
    • tablespoons white sugar
  • 1 ½ teaspoons dried basil leaves
    • 1 ½
    • teaspoons dried basil leaves
  • ¼ teaspoon black pepper
    • ¼
    • teaspoon black pepper

​

After a little digging I realized there is a 'thin space' (\u2009) between the whole & fractional symbol too. Is it possible to do what I want with a single regular expression? The pattern I was trying to use is something like this: `([\d\u00BC-\u00BE\u2150-\u215E]? )`

​

Open to any other ideas as well.

6 comments · 67% upvoted · other discussions

comments

eclunrcpp

It is possible, try https://regexr.com/ to tune your regex.

Also keep in mind if things are variable and it is giving you trouble on the regex (for example if some numbers had the unicode space between the integer and fraction but not others), you can run a replace on the strings first to get rid of it, and then run them through the regex

aneurysm_ [OP]

yeah i've been using regexr. right now i just don't know how to use all the look-ahead options necessary to capture all the possible cases. In the past each time I have needed to use regex a pattern the necessary pattern has been pretty clear.

Im gonna try to find a book or a video about regex patterns I think, but thank you

eclunrcpp

Can you give an example of the lookahead you are trying to do?

ASIC_SP

If you are okay with third-party regex module, you can use \p{N} to match any kind of digit.

aneurysm_ [OP]

i'm not familiar with the regex module but doesn't the \d match digits?

If not, is there a similar way to do that using re or is that additional functionality exclusive to that module?

ASIC_SP

\d won't match digits like ½

\p{N} is unicode aware, and yeah this isn't available in re module.