如何使用preg_match_all捕获带有正斜杠的空格后跟数字?

时间:2017-10-19 14:28:04

标签: regex

我正在尝试将以下格式DECLARE @Person VARCHAR(255) SET @Person = 'Person' print '----------------------------------------------------------------------------------------------------' if object_id(@Person, 'U') is not null begin print ' Tabelle ' + @Person + ' wird entfernt' execute('drop table ' + @Person) end GO 捕获到preg_match_all返回的相同键值中。

这就是我提出的:

1 1/4

输出:

<strong>1 1/2 lbs</strong> fish
<strong>⅛ cup</strong> peppers
<strong>1-2 tbsp</strong> olive Oil

preg_match_all("/[\d+↉½⅓⅔¼¾⅕⅖⅗⅘⅙⅚⅐⅛⅜⅝⅞⅑⅒⅟](?:\/\d+?)?(?:-?\d+)?(?: \/\d+)?/u", $input_lines, $output_array);

但它应该是:

0 => array(
    0 => 1
    1 => 1/2
    2 => ⅛
    3 => 1-2
    )
)

http://www.phpliveregex.com/p/lHv

1 个答案:

答案 0 :(得分:1)

(?:[ \-\/]*[\d↉½⅓⅔¼¾⅕⅖⅗⅘⅙⅚⅐⅛⅜⅝⅞⅑⅒⅟]+)+

匹配您的示例并匹配

之类的字符串
<strong>15½ cup</strong> peppers
<strong>14½-17 cup</strong> peppers
<strong>15 1/2 - 16    tbsp</strong> Oil
<strong>1½</strong> Oil

如果你想避免<strong>1 1/2 lbs</strong> f15h的双重匹配,你可以检查带有正面背后的“强”标签

(?<=<strong>)(?:[ \-\/]*[\d↉½⅓⅔¼¾⅕⅖⅗⅘⅙⅚⅐⅛⅜⅝⅞⅑⅒⅟]+)+