如何使用正则表达式使用PHP提取数据(字符串)?

时间:2011-10-09 14:12:39

标签: php regex

我试图提取

$str = "Instant Oatmeal - Corn Flavour 175g (35g x 5)";
preg_match('/(?P<name>.*) (?P<total_weight>\d+)(?P<total_weight_unit>.*) \((?P<unitWeight>\d+)(?P<unitWeight_unit>.*) x (?P<portion_no>\d+)\)/', $str, $m);

这是正确的:

Instant Oatmeal - Corn Flavour 175g (35g x 5)
name : Instant Oatmeal - Corn Flavour
total_weight : 175 g
#portion : 5
unit weight : 35 g

但是,如果我想提取

$str = "Cholcolate Sandwich Cookies (Tray) 264.6g (29.4g x 9)";

不正确:

Cholcolate Sandwich Cookies (Tray) 264.6g (29.4g x 9)
name : Cholcolate Sandwich Cookies (Tray)
total_weight : 264 .6g
#portion : 9
unit weight : 29 .4g

如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

对非重要的正则表达式使用自由间隔模式!

在处理像这样的非平凡正则表达式时,您可以通过使用大量注释(以及任何嵌套括号的缩进)以自由间距格式编写它们来显着提高可读性(和可维护性)。这是您的自由间距格式的原始正则表达式,带有注释:

$re_orig = '/# Original regex with added comments.
    (?P<name>.*)               # $name:
    [ ]                        # Space separates name from weight.
    (?P<total_weight>\d+)      # $total_weight:
    (?P<total_weight_unit>.*)  # $total_weight_unit:
    [ ]                        # Space separates totalunits from .
    \(                         # Literal parens enclosing portions data.
    (?P<unitWeight>\d+)        # $unitWeight:
    (?P<unitWeight_unit>.*)    # $unitWeight_unit:
    [ ]x[ ]                    # "space-X-space" separates portions data.
    (?P<portion_no>\d+)        # $portion_no:
    \)                         # Literal parens enclosing portions data.
    /x';

以下是改进版本:

$re_improved = '/# Match Name, total weight, units and portions data.
    ^                       # Anchor to start of string.
    (?P<name>.*?)           # $name:
    [ ]+                    # Space(s) separate name from weight.
    (?P<total_weight>       # $total_weight:
      \d+                   # Required integer portion.
      (?:\.\d*)?            # Optional fractional portion.
    )
    (?P<total_weight_unit>  # $total_weight_unit:
      .+?                   # Units consist of any chars.
    )
    [ ]+                    # Space(s) separate total from portions.
    \(                      # Literal parens enclosing portions data.
    (?P<unitWeight>         # $unitWeight:
      \d+                   # Required integer portion.
      (?:\.\d*)?            # Optional fractional portion.
    )
    (?P<unitWeight_unit>    # $unitWeight_unit:
      .+?                   # Units consist of any chars.
    )
    [ ]+x[ ]+               # "space-X-space" separates portions data.
    (?P<portion_no>         # $portion_no:
      \d+                   # Required integer portion.
      (?:\.\d*)?            # Optional fractional portion.
    )
    \)                      # Literal parens enclosing portions data.
    $                       # Anchor to end of string.
    /xi';

注意:

  • 所有数值的表达式都已改进,以允许可选的小数部分。
  • 添加字符串锚点的开始和结束。
  • 如果部分数据中的i为大写,则添加X ignorecase修饰符。

我不确定你是如何应用这个正则表达式的,但这个改进的正则表达式应该可以解决你的问题。

编辑次数:2011-10-09 11:17 MDT 更改单位的表达方式更加宽松,以便允许Ilmari Karonen指出的案例。

答案 1 :(得分:2)

使用此:

/(?P<name>.*) (?P<total_weight>\b[0-9]*\.?[0-9]+)(?P<total_weight_unit>.*) \((?P<unitWeight>\b[0-9]*\.?[0-9]+)(?P<unitWeight_unit>.*) x (?P<portion_no>\d+)\)/

您的问题是您没有考虑浮点数。我纠正了这个。请注意,该部分仍然是一个整数,但我想这是合乎逻辑的:)