我不知道如何给第二组一个字符串。现在I have Full Watch and Group 1(not completely)和var body: some View {
NavigationView {
List {
Text("Does not work")
}
.navigationBarItems(trailing:
Button(action: {
self.showDetail.toggle()
}) {
Image(systemName: "chevron.right.circle")
.imageScale(.large)
.rotationEffect(.degrees(showDetail ? 90 : 0))
.scaleEffect(showDetail ? 1.5 : 1)
.padding()
.animation(.spring())
}
)
}
}
示例字符串:
(?:<p>)?@preview\((.*)\)(?:<\/p>)?
完全匹配:
@preview(example-component/example-component)
<p>@preview(example-component/example-component)</p>
@preview(example-component/example-component, title="sadad" text="asd")
@preview(example-component/example-component, title="sadad" text="asd" )
或
@preview(example-component/example-component)
或
<p>@preview(example-component/example-component)</p>
第1组:
@preview(example-component/example-component, title="sadad" text="asd")
第2组:
example-component/example-component
谢谢
答案 0 :(得分:1)
您可以使用
\(([^,)]+)(?:,\s*([^)]+))?
\( # match a "(" literally
([^,)]+) # not a comma nor a ) -> group 1
(?:,\s* # a non-capturing group, followed by whitespaces
([^)]+) # not a ) -> group 2
)? # thw whole term is optional
在JavaScript
中:
let strings = ['@preview(example-component/example-component)',
'<p>@preview(example-component/example-component)</p>',
'@preview(example-component/example-component, title="sadad" text="asd")',
'@preview(example-component/example-component, title="sadad" text="asd" )'];
let rx = /\(([^,)]+)(?:,\s*([^)]+))?/;
strings.forEach(function(item) {
let m = item.match(rx);
if (typeof(m[2]) !== "undefined") {
console.log(m[2]);
}
});
答案 1 :(得分:1)
您的表达式只有一个匹配组,可以匹配()
中的所有内容,您只需要根据昏迷将其分为两组即可。
(?:<p>)?@preview\((.*?)(?:,\s*(.*?))?\)(?:<\/p>)?
我将(.*)
更改为(.*?)(?:,\s*(.*?))?
(.*?)
非贪婪所有选择器可以匹配所有内容,非贪婪会使它停止在找到的第一个昏迷中
(?:,\s*(.*?))?
非捕获组可捕获上一个组之后的所有内容,包括,
,以使用?
(.*?)
第二个非贪心所有选择器捕获,
之后的所有内容,不包括空格