0

我正在尝试读取 php 中的文件并捕获匹配数组。我有一个适合我想要的模式,但前提是我在文件中的某个点之后开始匹配 - 之后就可以了。

我正在阅读一个看起来像这样的 .less 文件:

/* ... */

.icon-flip-vertical:before {
  -webkit-transform: scale(1, -1);
  -moz-transform: scale(1, -1);
  -ms-transform: scale(1, -1);
  -o-transform: scale(1, -1);
  transform: scale(1, -1);
}
/* ensure rotation occurs inside anchor tags */
a .icon-rotate-90:before,
a .icon-rotate-180:before,
a .icon-rotate-270:before,
a .icon-flip-horizontal:before,
a .icon-flip-vertical:before {
  display: inline-block;
}

/* start capturing with pattern here */

.icon-glass:before{content:"\f000";}
.icon-music:before{content:"\f001";}
.icon-search:before{content:"\f002";}
.icon-envelope-alt:before{content:"\f003";}

/* ... more like this after */

我试图捕捉图标类的“肉”。即:.icon-XXXX:before\.icon-([^:]+)成功地使用了该模式,但就像我说的,我想在文件中的指定点开始匹配。

一个重要的注意事项是,由于某种原因,并非所有图标类都在自己的行中。

我在想是否可以将匹配设置为从包含可以解决问题的字符串的行开始\f000。如何做到这一点,我还没有找到。

提前感谢大家的时间!

4

2 回答 2

0

Before applying your regex matching, extract the piece of string you wanna work on:

$subString = substr($string,strpos($string,"start capturing with pattern here"));

Then, apply your regex on that substring.

Please note that for using this approach, you must pick a separator (in my example, start capturing with pattern here) that breaks the string in two pieces: a useless one and the useful one.

于 2013-07-15T00:08:20.243 回答
0

您可以尝试使用前瞻断言。就像是:

(?<=\.icon-)[\w-]+(?=:before[^\w-][^}]*?"\\\\f00)

应该与后面的单词完全匹配.icon-,但前提是该单词后面也跟着:before一个包含"\f00.

请注意,这假设您的 CSS 没有语法错误(特别是没有未闭合的块)。如果是这样,正则表达式可能无法处理它。

于 2013-07-15T00:18:41.073 回答