尝试为以下模式创建正则表达式

发布于 2024-10-09 03:16:18 字数 337 浏览 1 评论 0原文

以下是模式:

Red,Green (and so on...)
Red (+5.00),Green (+6.00) (and so on...)
Red (+5.00,+10.00),Green (+6.00,+20.00) (and so on...)
Red (+5.00),Green  (and so on...)

每个属性(“红色”、“绿色”)可以有 0、1 或 2 个修饰符(显示为“+5.00、+10.00”等)。

我需要捕获每个属性及其修饰符作为单个字符串(即“红色(+5.00,+10.00)”,“绿色(+6.00,+20.00)”。

帮助?

Here are the patterns:

Red,Green (and so on...)
Red (+5.00),Green (+6.00) (and so on...)
Red (+5.00,+10.00),Green (+6.00,+20.00) (and so on...)
Red (+5.00),Green  (and so on...)

Each attribute ("Red,"Green") can have 0, 1, or 2 modifiers (shown as "+5.00,+10.00", etc.).

I need to capture each of the attributes and their modifiers as a single string (i.e. "Red (+5.00,+10.00)", "Green (+6.00,+20.00)".

Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[浮城] 2024-10-16 03:16:18

另一个例子(PCRE):

((?:Red|Green)(?:\s\((?:\+\d+\.\d+,?)+\))?)

说明:

(...)              //  a capture group
  (?:...)          // a non-capturing group
    Read|Green     // matches Red or Green
  (?:...)?         // an optional non-capturing group
    \s             // matches any whitespace character
    \(             // matches a literal (
    (?:...)+       // a non-capturing group that can occur one or more times
      \+           // matches a literal +
      \d+          // matches one or more digits
      \.           // matches a literal .
      \d+          // matches one or more digits
      ,?           // matches an optional comma
    \)             //matches a literal )

更新:
或者实际上,如果您只想提取数据,那么

((?:Red|Green)(?:\s\([^)]+\))?)

就足够了。

更新 2: 正如您的评论中所指出的,这将匹配第一部分中除 ,(:

([^,(]+(?:\s\([^)]+\))?)

(不起作用,太宽松)

更具限制性(仅允许字符和数字,您可以只使用 \w:

(\w+(?:\s\([^)]+\))?)

更新 3

我明白了,我的第一个替代方案无法正常工作,但 \w 有效:

$pattern = "#\w+(?:\s\([^)]+\))?#";
$str = "foo (+15.00,-10.00),bar (-10.00,+25),baz,bing,bam (150.00,-5000.00)";

$matches = array();

preg_match_all($pattern, $str, $matches);

print_r($matches);

打印

Array
(
    [0] => Array
        (
            [0] => foo (+15.00,-10.00)
            [1] => bar (-10.00,+25)
            [2] => baz
            [3] => bing
            [4] => bam (150.00,-5000.00)
        )

)

更新 4:

好的,我明白了有些东西有效,请检查它是否总是有效:

(?=[^-+,.]+)[^(),]+(?:\s?\((?:[-+\d.]+,?)+\))?

使用:

$pattern = "#(?=[^-+,.]+)[^(),]+(?:\s?\((?:[-+\d.]+,?)+\))?#";
$str = "5 lb. (+15.00,-10.00),bar (-10.00,+25),baz,bing,bam (150.00,-5000.00)";

preg_match_all给我

Array
(
    [0] => Array
        (
            [0] => 5 lb. (+15.00,-10.00)
            [1] => bar (-10.00,+25)
            [2] => baz
            [3] => bing
            [4] => bam (150.00,-5000.00)
        )

)

也许有一个更简单的正则表达式,我不是专家......

Another example (PCRE):

((?:Red|Green)(?:\s\((?:\+\d+\.\d+,?)+\))?)

Explanation:

(...)              //  a capture group
  (?:...)          // a non-capturing group
    Read|Green     // matches Red or Green
  (?:...)?         // an optional non-capturing group
    \s             // matches any whitespace character
    \(             // matches a literal (
    (?:...)+       // a non-capturing group that can occur one or more times
      \+           // matches a literal +
      \d+          // matches one or more digits
      \.           // matches a literal .
      \d+          // matches one or more digits
      ,?           // matches an optional comma
    \)             //matches a literal )

Update:
Or actually if you just want to extract the data, then

((?:Red|Green)(?:\s\([^)]+\))?)

would be sufficient.

Update 2: As pointed out in your comment, this would match anything in the first part but , and (:

([^,(]+(?:\s\([^)]+\))?)

(does not work, too permissive)

to be more restrictive (allowing only characters and numbers, you can just use \w:

(\w+(?:\s\([^)]+\))?)

Update 3:

I see, the first of my alternatives does not work correctly, but \w works:

$pattern = "#\w+(?:\s\([^)]+\))?#";
$str = "foo (+15.00,-10.00),bar (-10.00,+25),baz,bing,bam (150.00,-5000.00)";

$matches = array();

preg_match_all($pattern, $str, $matches);

print_r($matches);

prints

Array
(
    [0] => Array
        (
            [0] => foo (+15.00,-10.00)
            [1] => bar (-10.00,+25)
            [2] => baz
            [3] => bing
            [4] => bam (150.00,-5000.00)
        )

)

Update 4:

Ok, I got something working, please check whether it always works:

(?=[^-+,.]+)[^(),]+(?:\s?\((?:[-+\d.]+,?)+\))?

With:

$pattern = "#(?=[^-+,.]+)[^(),]+(?:\s?\((?:[-+\d.]+,?)+\))?#";
$str = "5 lb. (+15.00,-10.00),bar (-10.00,+25),baz,bing,bam (150.00,-5000.00)";

preg_match_all gives me

Array
(
    [0] => Array
        (
            [0] => 5 lb. (+15.00,-10.00)
            [1] => bar (-10.00,+25)
            [2] => baz
            [3] => bing
            [4] => bam (150.00,-5000.00)
        )

)

Maybe there is a simpler regex, I'm not an expert...

撩起发的微风 2024-10-16 03:16:18

PCRE 格式:

(Red|Green)(\s\((?P<val1>.+?)(,){0,1}(?P<val2>.+?){0,1}\)){0,1}

来自 PHP 的匹配:

preg_match_all("/(Red|Green)(\s\((?P<val1>.+?)(,){0,1}(?P<val2>.+?){0,1}\)){0,1}/ims", $text, $matches);

PCRE format:

(Red|Green)(\s\((?P<val1>.+?)(,){0,1}(?P<val2>.+?){0,1}\)){0,1}

Match from PHP:

preg_match_all("/(Red|Green)(\s\((?P<val1>.+?)(,){0,1}(?P<val2>.+?){0,1}\)){0,1}/ims", $text, $matches);
冷︶言冷语的世界 2024-10-16 03:16:18

这是我的出价:

/
  (?:^|,)                   # Match line beginning or a comma
  (?:                       # parent wrapper to catch multiple "color (+#.##)" patterns
    (                         # grouping pattern for picking off matches
      (?:(?:Red|Green),?)+      # match the color prefix
      \s\(                      # space then parenthesis
      (?:                       # wrapper for repeated number groups
        (?:\x2B\d+\.\d+)          # pattern for the +#.##
      ,?)+                      # end wrapper
      \)                        # closing parenthesis
    )+                        # end matching pattern
  )+                        # end parent wrapper
/

翻译为:

/(?:^|,)(?:((?:(?:Red|Green),?)+\s\((?:(?:\x2B\d+\.\d+),?)+\))+)+/

编辑

抱歉,它之前只捕获了最后一个模式。这将捕获所有匹配(或应该)。

Here's my bid:

/
  (?:^|,)                   # Match line beginning or a comma
  (?:                       # parent wrapper to catch multiple "color (+#.##)" patterns
    (                         # grouping pattern for picking off matches
      (?:(?:Red|Green),?)+      # match the color prefix
      \s\(                      # space then parenthesis
      (?:                       # wrapper for repeated number groups
        (?:\x2B\d+\.\d+)          # pattern for the +#.##
      ,?)+                      # end wrapper
      \)                        # closing parenthesis
    )+                        # end matching pattern
  )+                        # end parent wrapper
/

Which translates to:

/(?:^|,)(?:((?:(?:Red|Green),?)+\s\((?:(?:\x2B\d+\.\d+),?)+\))+)+/

EDIT

Sorry, it was only catching the last pattern before. This will catch all matches (or should).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文