正则表达式帮助,贪婪与非贪婪 [英] Regex help, greedy vs. non-greedy

查看:97
本文介绍了正则表达式帮助,贪婪与非贪婪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个大的html字符串,例如

Hey all I have a large html string like

<a style="background: rgb(100, 101, 43) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-backg    round-inline-policy: -moz-initial;" href="#">swatch4</a>  
<a style="background: rgb(34, 68, 33) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-b    ackground-inline-policy: -moz-initial;" href="#">swatch5</a>  
<a style="background: rgb(11, 38, 68) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -m    oz-background-inline-policy: -moz-initial;" href="#">swatch6</a>  
<a style="background: rgb(39, 11, 60) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial    ; -moz-background-inline-policy: -moz-initial;" href="#">swatch7</a>

...这些条目很多...

...There are lot of these entries...

我想最终得到的是一个php数组

What I would like to end up with is a php array

$colors = array(
 'rgb(34, 34, 33)',
 'rgb(11, 38, 68)',
...
); 

我需要帮助提取字符串的rgb部分的步骤.我的正则表达式在吃太多的弦时遇到麻烦.任何帮助将不胜感激,我想记下来并学习如何做(而不是仅仅完成它).我正在使用vim btw.

The step I need help with extracting the rgb part of the string. I am having trouble with my regexs eating too much of the string. Anyhelp would be appreciated, i would like to get this down and learn how to do (as opposed to just having it done). I am using vim btw.

谢谢

推荐答案

简单的正则表达式是

(rgb\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*\))

每个"\ s * \ d {1,3} \ s *"表示:

Each "\s*\d{1,3}\s*," means:

  • 匹配空间(如molf在注释中所说的''或制表符等空格,其范围为0到inf)
  • 匹配一个数字(从1到3,因为您从0到255)
  • 匹配其他空格
  • 匹配逗号(,)

您可以将Paolo的代码用于数组部分.

You can use Paolo's code for the array part.

这篇关于正则表达式帮助,贪婪与非贪婪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆