简体   繁体   English

正则表达式在比赛开始和结束之间获取字符串

[英]regex get string between start and end of match

I'm trying to take classes from a css file using regex. 我正在尝试使用正则表达式从css文件中获取类。 I need to take classes between start and end of match string because most of the class are starting with the same prefix like myy- classname followed by its rules so i'm giving the { as end point, because i need to take only the class name. 我需要在匹配字符串的开始和结尾之间选择类,因为大多数类都以相同的前缀开头,例如myy- classname及其规则,因此我将{作为结束点,因为我只需要选择类名称。 For Example 例如

   .myy-foobar{
         /*CSS Rules goes here*/
         }
   .myy-anotherbar{
         /*CSS Rules goes here*/
         }

Now i need to take foobar,anotherbar using regex, so i used this preg_match_all 现在我需要使用正则表达式来使用foob​​ar,anotherbar ,所以我使用了这个preg_match_all

preg_match_all('/^myy-[(.*)]{$/', $file, $match);
var_dump($match);

To my understanding it should start with myy- so i put ^myy- and any character between this so i used [] and end with { so i used {$ 据我了解,它应该以myy开头,因此我将^myy-以及此之间的任何字符都放在^myy- ,因此我使用了[]并以{结尾,所以我使用了{$

and this 和这个

preg_match_all('/(myy-)(.*)({)/', $file, $match);
var_dump($match);

Here too same logic(start with myy- and any character and end with {) i tried with different syntax but nothing helps. 在这里,我也尝试了相同的逻辑(以myy-和任何字符开头,以{结尾),但我使用了不同的语法,但没有帮助。

There are lot of classes in the file so i need to take only the middle part of all class as i know the prefix already. 文件中有很多类,因此我只需要接受所有类的中间部分,因为我已经知道前缀了。

Can anyone help on this? 有人可以帮忙吗?

To match just the name, use \\.myy-\\K[^{\\s]+(?=\\s*{) 要仅匹配名称,请使用\\.myy-\\K[^{\\s]+(?=\\s*{)

$regex = '~\.myy-\K[^{\s]+(?=\s*{)~';
preg_match_all($regex, $yourstring, $matches);
print_r($matches[0]);

See the matches in the Regex Demo . 在正则表达式演示中查看比赛。

Explanation 说明

  • \\.myy- matches .myy- \\.myy-匹配.myy-
  • The \\K tells the engine to drop what was matched so far from the final match it returns \\K告诉引擎放弃与最终匹配相距甚远的匹配,它返回
  • [^{\\s]+ matches any chars that are not a { or a white-space char (this is the match) [^{\\s]+匹配所有非{或空格字符的字符(这是匹配项)
  • The lookahead (?=\\s*{) asserts that what follows is optional whitespace chars and a { 前瞻(?=\\s*{)断言,后面是可选的空白字符和{

To match the whole class use this: \\.myy-.*?} 要匹配整个类,请使用以下命令: \\.myy-.*?}

$regex = '~\.myy-.*?}s~';
preg_match_all($regex, $yourstring, $matches);
print_r($matches[0]);

Longest Match. 最长的比赛。 The greedy .* first matches the whole string, then backtracks only as far as needed to allow the next token to match. 贪婪的.*首先匹配整个字符串,然后仅回溯所需的距离以允许下一个标记匹配。 Therefore, you will often overshoot the place where you want the match to end (longest match). 因此,您经常会超出您希望比赛结束的地方(最长的比赛)。

Shortest Match. 最短比赛。 The lazy .*? 懒的.*? (made "lazy" by the ? ) means that the dot only matches as many characters as needed to allow the next token to match (shortest match). (由?表示“惰性”)表示点仅匹配所需数量的字符,以允许下一个标记匹配(最短匹配)。

Reference 参考

The below code would match the text after .myy- upto the next { symbol , 以下代码将匹配.myy-之后的文本,直到下一个{符号,

preg_match_all('~\.myy-\K[^{]*(?={)~', $file, $match);
var_dump($match);

DEMO 演示

OR 要么

The below code would match the text after .myy- upto the next { symbol or : symbol. 下面的代码将匹配.myy-之后的文本,直到下一个{符号或:符号。

preg_match_all('~\.myy-\K[^{:]*(?={|:)~', $file, $match);
var_dump($match);

DEMO 演示

Explanation: 说明:

  • \\.myy- Matches the string .myy- \\.myy-匹配字符串.myy-
  • \\K discards the previously matched characters. \\K丢弃先前匹配的字符。
  • [^{]* Matches any charcter not of { zero or more times. [^{]*匹配不等于{零次或多次的任何字符。
  • (?={) Only when the following charcter must be a { open curly brace. (?={)仅当以下字符必须为{打开花括号时。 (?=) is called positive look-ahead (?=)称为正向超前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM