值两侧的测量单位的正则表达式 (200 g/g 200)
Regex for measuraments unit on both sides of the value (200 g/g 200)
考虑到单位可以在数字之前或之后,我正在尝试编写一个正则表达式来捕获字符串中的任何度量单位。
我暂时想到的是两个正则表达式。
/\d*\.?,?\d+\s?(kg|g|l)/gi
匹配
ABC 200g
EFG 5,4 Kg
HIL 2x20l
和 (kg|g|l)\s?\d+,?\.?d*
匹配:
ABC g200
EFG kg 5,4
HIL l 20x2
如何加入两个正则表达式以匹配两者:
ABC g200
EFG 5,4 Kg
使用不区分大小写的模式,匹配可选的 k
和 g
或 l
以及交替 |
以匹配其他方式的模式。
可选的点和逗号可以在一个字符中 class [.,]?
否则 .?,?
也可以同时匹配 .,
单词边界 \b
防止单元后的部分匹配。
\d*[.,]?\d+\s*(?:k?g|l)\b|\b(?:k?g|l)\s*\d*[.,]?\d+
使用您展示的示例,请尝试使用正则表达式。
(?:(?:(?:\d+)g|(?:g\d+))|(?:(?:l\s*\d+)|(?:\d+\s*l))|(?:(?:\d+,\d+\s*Kg)|(?:kg\s*\d+,\d+)))
解释: 为以上添加详细解释。
(?: ##Starting 1st capturing group from here.
(?: ##Starting 2nd capturing group from here.
(?:\d+)g|(?:g\d+) ##Matching either digits followed by g OR g followed by digits(both conditions in non-capturing groups here).
) ##Closing 2nd capturing group here.
| ##Putting OR condition here.
(?: ##Starting 3rd capturing group here.
(?:l\s*\d+)|(?:\d+\s*l) ##Matching eiter l followed by 0 or more spaces followed by digits OR digits followed by 0 or more spaces followed by l.
) ##Closing 3rd capturing group here.
| ##Putting OR condition here.
(?: ##Starting 4th capturing group here.
(?:\d+,\d+\s*Kg)|(?:kg\s*\d+,\d+) ##Checking either digits followed by comma digits spaces Kg OR kg spaces digits comma digits here.
) ##Closing 4th capturing group here.
) ##Closing 1st capturing group here.
考虑到单位可以在数字之前或之后,我正在尝试编写一个正则表达式来捕获字符串中的任何度量单位。
我暂时想到的是两个正则表达式。
/\d*\.?,?\d+\s?(kg|g|l)/gi
匹配
ABC 200g
EFG 5,4 Kg
HIL 2x20l
和 (kg|g|l)\s?\d+,?\.?d*
匹配:
ABC g200
EFG kg 5,4
HIL l 20x2
如何加入两个正则表达式以匹配两者:
ABC g200
EFG 5,4 Kg
使用不区分大小写的模式,匹配可选的 k
和 g
或 l
以及交替 |
以匹配其他方式的模式。
可选的点和逗号可以在一个字符中 class [.,]?
否则 .?,?
也可以同时匹配 .,
单词边界 \b
防止单元后的部分匹配。
\d*[.,]?\d+\s*(?:k?g|l)\b|\b(?:k?g|l)\s*\d*[.,]?\d+
使用您展示的示例,请尝试使用正则表达式。
(?:(?:(?:\d+)g|(?:g\d+))|(?:(?:l\s*\d+)|(?:\d+\s*l))|(?:(?:\d+,\d+\s*Kg)|(?:kg\s*\d+,\d+)))
解释: 为以上添加详细解释。
(?: ##Starting 1st capturing group from here.
(?: ##Starting 2nd capturing group from here.
(?:\d+)g|(?:g\d+) ##Matching either digits followed by g OR g followed by digits(both conditions in non-capturing groups here).
) ##Closing 2nd capturing group here.
| ##Putting OR condition here.
(?: ##Starting 3rd capturing group here.
(?:l\s*\d+)|(?:\d+\s*l) ##Matching eiter l followed by 0 or more spaces followed by digits OR digits followed by 0 or more spaces followed by l.
) ##Closing 3rd capturing group here.
| ##Putting OR condition here.
(?: ##Starting 4th capturing group here.
(?:\d+,\d+\s*Kg)|(?:kg\s*\d+,\d+) ##Checking either digits followed by comma digits spaces Kg OR kg spaces digits comma digits here.
) ##Closing 4th capturing group here.
) ##Closing 1st capturing group here.