我们有一个swiftlint规则,它基本上对我们的结构,类和协议(DC,DB或DM,取决于函数)强制执行前缀。我们希望仅将此匹配到顶级对象,例如在下面的示例中,我只想 <title>Images</title>
<link rel="stylesheet" type="text/css" href="styles/main.css">
<link href="https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz" rel="stylesheet">
<body>
<div class="content">
<header class="artnames">
<a href="#"><h1 id="myphotos">MY PHOTOS</h1></a>
<a href="#"><h1 id="aboutme">ABOUT ME</h1></a>
</header>
<div class="photos">
<img src="images/streetart.jpg" style="width:50%; float: left; clear: left;">
<img src="images/streetart3.jpg" style="width:50%; float: right;">
<img src="images/streetart2.jpg" style="width:50%; float: left;">
<img src="images/streetart4.jpg" style="width:50%; float: right;">
<img src="images/streetart5.jpg" style="width:50%; margin-top: -3px; float: left;">
</div>
</div>
<div class="footerr">
<p>I love when nothing works, aaaaa I love when nothing works, aaaaa I love when nothing works, aaaaa I love when nothing works, aaaaa I love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaalove when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaalove when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaalove when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaalove when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaalove when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaalove when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaalove when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaalove when nothing works, aaaaaI love when nothing works, aaaaaI love when nothing works, aaaaa</p>
</div>
</body>
和TopLevel
抛出错误:
SecondTopLevel
我们当前的正则表达式如下:class TopLevel {
struct NotTopLevel {
var test
}
}
class SecondTopLevel {
}
,这将匹配(struct|class|protocol)\s(?!(func|DB|DC|DM))[^\n\r]+\{
和TopLevel
。我想以某种方式给一些正则表达式(或其他一些swiftlint工具)与NotTopLevel
不匹配,但仍匹配NotTopLevel
和TopLevel
。这可能吗?有关如何做到这一点的任何想法?
我一直试图模仿的方法是在检查我们的前缀之前查找0个或更多先前的类定义,但是,我遇到了在检查之前尝试匹配整个类一次或多次的问题前缀。
答案 0 :(得分:2)
您可以使用
(struct|class|protocol).*
所以你只匹配第一个结构类或协议。