匹配并非以选择器

时间:2017-08-22 13:48:35

标签: css regex regex-negation

如果我有一个css文件,就像这样(例如):

div {
  color: red;
}
a{color: red}.someClass {color: red}

.someOtherClass {
color: red;
}

@media (max-width: 767px){
 div { color: green; }
 [data-module="123"] .someOtherClass { color: green; }
}

[data-module='123'] .someRandomClass { color: red }
[data-module='123'] .someOtherRandomClass {
color: red;
}

只有最后三个才能与我们公司正确使用CSS相匹配:

[data-module='123'] .someOtherClass {
[data-module="123"] .someRandomClass {
[data-module='123'] .someOtherRandomClass {

因为它们以[data-module =' xxx']开头(注意用户可以使用"或') 我已经能够正确地找到/编辑与这三个最后三个匹配的REGEX,但我需要相反,我想匹配不以[data-module =" xxx"]开头的选择器...因此,当我使用PHP / Regex进行检查时,如果匹配,我将告诉用户他没有遵循规则...正则表达式必须考虑这个CSS可能是无组织的,如示例中所示也可能有媒体查询..

任何神奇的人都可以帮我解决这个问题吗?因为这是神奇的......某种来源......无法弄清楚

编辑: 我想要这样的正则表达式: http://regexr.com/328s7

但只有匹配,如果选择器之前没有[data-module =" xxx"]

1 个答案:

答案 0 :(得分:0)

Notes

The additional information (in comments below the question) allowed me to properly create a regex that will work for your situation.

Please note that this answer can be made better by following true CSS selector rules as well as declaration/declaration list/declaration block rules, however, considering the CSS selector syntax importance in this question, I've decided to disregard it and accept all characters (except for newline and curly brackets). This regex will not work on any lines where there is a curly bracket before the selectors (so make sure you've beautified your CSS before with some sort of auto-formatting tool if you tend to write sloppy code).

Note that the regex below requires the two following modifiers:

  • Global: grails.plugin.springsecurity.useSecurityEventListener = true
  • Multi line: class MySecurityEventListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> { @Autowired UserService userService void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) { userService.postLoginMessage(event.authentication.principal.username, null) } }

Answer

Regex

Set-AzureDeployment : An error occurred while sending the request.
At <my script>
+     $setdeployment = Set-AzureDeployment `
+                      ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Set-AzureDeployment], HttpRequestException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.HostedServices.SetAzureDeploymentCommand


To explain a bit of what this regex is doing:

  1. Assert position at start of a line
  2. Match between zero and unlimited times the carriage return g or line-feed m characters
  3. Match between one and unlimited times, but as few times as possible, characters that do not appear in the grouping (^([\r\n]*)((?!\[data-module=(['"])\d+\3\])[^\r\n{}])+({[\s\S]*?})$ ) and that does not contain the structure of \r, followed by either a single quote \n or double quote \r\n{} character, followed by between one and unlimited digits, followed by the same previously matched character (single or double quote - ensures it was properly closed), and finally by a closing square bracket [data-module=
  4. Match the CSS declaration block by checking for an opening curly bracket ' followed by any character (including new line characters) between zero and unlimited times, but as few times as possible, followed by a closing curly bracket "
  5. Assert position at the end of the line (not really needed here, but I like the conformity with the first step)

Update

Regex

]

This one will also catch at-rules. Note that it will not catch the initial at-rule, however.

For example if you have the following code:

{

In the above example, only the third, fifth, and sixth lines will be caught. The first line } will not be captured.