简体   繁体   English

PHP Regex匹配多行字符串中的多次出现

[英]PHP Regex Match Multiple Occurrences in Multiple Lines String

I have this multiple lines CSS style: 我有以下多行CSS样式:

.style-1 {
    border-color: #2980B9;
}

.style-2 {
    background-color: #2980B9;
    #FFF;
}

.style-3 input[type=submit] {
    background-color: #2980B9;
    color: #FFF;
}

And here is my regex pattern: \\..*\\{\\s?\\s?+.*\\s?\\}\\s?\\s?+ 这是我的正则表达式模式: \\..*\\{\\s?\\s?+.*\\s?\\}\\s?\\s?+

https://regex101.com/r/tP7qE7/1 https://regex101.com/r/tP7qE7/1

I want to match each rule from the selector until the closing bracket so I can prepend new selector to each rule and echo each rule separately. 我想匹配从选择器到结束括号的每个规则,因此我可以在每个规则之前添加新的选择器并分别回显每个规则。 Right now with my regex pattern above I can get only the first rule. 现在,使用上面的正则表达式模式,我只能得到第一个规则。 I tried both preg_match() and preg_match_all() but with no success. 我尝试了preg_match()preg_match_all()没有成功。 How can I match each rule using regex? 如何使用正则表达式匹配每个规则?

Thank you in advance! 先感谢您!

You can use the following regex ( \\..*?\\{.*?\\} ) with g and s modifiers: 您可以将以下正则表达式( \\..*?\\{.*?\\} )与gs修饰符一起使用:

/\..*?\{.*?\}/gs

See DEMO 演示

\..*?\{\s?\s?+[\s\S]*?\s?\}\s?\s?+

You can use this.See demo. 您可以使用它。请参阅演示。

https://regex101.com/r/tP7qE7/3 https://regex101.com/r/tP7qE7/3

您可以尝试此模式以匹配选择器

(?s)^\..*?\{.*?\}$

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

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