简体   繁体   中英

PHP Regex Match Multiple Occurrences in Multiple Lines String

I have this multiple lines CSS style:

.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?+

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. How can I match each rule using regex?

Thank you in advance!

You can use the following regex ( \\..*?\\{.*?\\} ) with g and s modifiers:

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

See DEMO

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

You can use this.See demo.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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