正则表达式 - 识别分数 [英] Regex - Identify Fractions

查看:60
本文介绍了正则表达式 - 识别分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用正则表达式从配方数据库的表单字段中识别一个分数.成分将被输入到两部分的表单字段中.字段一是数量,字段二是成分.然后,我需要将字段 1 分解为其小数部分以输入到数据库中.

I need to identify a fraction from a form field in a recipe database using a Regex. Ingredients will be entered in a two part form fields. Field one is the amount, Field two is the ingredient. I then need to break field one into its fractional components to input into the database.

可能的条目包括:1, 1/2, 1 1/2, 以及前面任何一个带有文字的东西,例如 1 杯或 1/2 汤匙.我预见的最难的是:[2 28 盎司.罐] 其中 2 是数字,28 盎司.罐头就是这个词.

Possible entries include: 1, 1/2, 1 1/2, and any of the previous with words attached such as 1 cup, or 1/2 tbsp. the hardest I foresee would be: [2 28 oz. cans] where 2 is the number, and 28 oz. cans would be the word.

我发现:(\b[0-9]{1,3}(?:,?[0-9]{3})*(?:.[0-9]{2})?\b)什么样的作品.我对 Regex 完全陌生,所以我只进行猜测和检查,我很难让它对我有用.

I have found: (\b[0-9]{1,3}(?:,?[0-9]{3})*(?:.[0-9]{2})?\b) which sort of works. I am completely new to Regex, so I am working on guess and check only, and I am having a hard time making it work for me.

问题 1:我还需要识别单词部分.单词部分也可以是多个单词,例如 2 个大罐子,其中大罐子就是单词部分.上面的正则表达式很好地识别了数字,但我想不出一种方法来获取表单字段的其余部分.例如 1 1/2 汤匙给我 1,1,2 但这就是全部,我也需要汤匙.我尝试使用这个 Regex 并使用 len 来减少原始值,减去前面的分数,但是由于 1/2 和 1/2 都是允许的,所以无法弄清楚要减去多少个点 (1/2应该从字符串的前面减去6,1/2应该从字符串的前面减去4,只是看1,2的正则表达式结果我不知道要减去多少).

Problem #1: I need to identify the word part as well. The word part can be multiple words as well, such as 2 large cans, where large cans would be the word part. The above Regex identifies the numbers very well, but I cant figure out a way to grab the rest of the form field. For example 1 1/2 tbsp gives me 1,1,2 but that is all, and I need tbsp as well. I tried to use this Regex and use len to cut the original down, subtracting the fraction off the front, but had problems since 1 / 2 and 1/2 are both allowed, so cant figure out how many spots to subtract (1 / 2 should subtract 6 from the front of the string, 1/2 should subtract 4 from the front of the string, and just looking at the regex results of 1,2 I cant tell howmany to subtract).

问题#2:这不是那么重要,但是有没有关于如何识别 [2 28 oz 罐] 问题的想法?上面的正则表达式拉出 2,28,这是不正确的,它应该只拉出 2,然后剩下的(28 盎司罐)将是问题 1 的解决方案有望找到的另一部分.

Problem #2: This isnt so important, but any ideas on how to identity the [2 28 oz cans] problem? The above Regex pulls 2,28 out which is not correct, it shoudl only pull 2 out and then the rest (28 oz cans) would be the other part that the solution to problem 1 will hopefully find.

推荐答案

这是一个正则表达式,将匹配混合数字、整数和条目的其余部分(成分,希望与任何无关数字):

Here's a regex that will match mixed numbers, whole numbers, and the rest of the entry (the ingredient, hopefully with any extraneous numbers):

^((\d+( \d+/\d+)?)|(\d+/\d+))( (.+))?$

例如,如果有 2 28 盎司罐,它将匹配:

So for example if had 2 28 ounce cans it would match:

group 1: 2
group 2: 2
group 3: 
group 4: 
group 5:  28 ounce cans
group 5: 28 ounce cans

您关心的组是 1 &5. 第 1 组 将始终包含数量(作为数字、分数或带分数的数字),第 6 组 将始终包含剩余文本(成分).

The groups you care about are 1 & 5. Group 1 will always contain the amount (as a number, fraction, or number with a fraction) and group 6 will always have the remaining text (the ingredient).

这篇关于正则表达式 - 识别分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆