尝试为以下模式创建正则表达式
以下是模式:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一个例子(PCRE):
说明:
更新:
或者实际上,如果您只想提取数据,那么
就足够了。
更新 2: 正如您的评论中所指出的,
这将匹配第一部分中除,
和(
:(不起作用,太宽松)更具限制性(仅允许字符和数字,您可以只使用
\w
:更新 3:
我明白了,我的第一个替代方案无法正常工作,但
\w
有效:打印
更新 4:
好的,我明白了有些东西有效,请检查它是否总是有效:
使用:
preg_match_all
给我也许有一个更简单的正则表达式,我不是专家......
Another example (PCRE):
Explanation:
Update:
Or actually if you just want to extract the data, then
would be sufficient.
Update 2: As pointed out in your comment,
this would match anything in the first part but,
and(
:(does not work, too permissive)to be more restrictive (allowing only characters and numbers, you can just use
\w
:Update 3:
I see, the first of my alternatives does not work correctly, but
\w
works:prints
Update 4:
Ok, I got something working, please check whether it always works:
With:
preg_match_all
gives meMaybe there is a simpler regex, I'm not an expert...
PCRE 格式:
来自 PHP 的匹配:
PCRE format:
Match from PHP:
这是我的出价:
翻译为:
编辑
抱歉,它之前只捕获了最后一个模式。这将捕获所有匹配(或应该)。
Here's my bid:
Which translates to:
EDIT
Sorry, it was only catching the last pattern before. This will catch all matches (or should).