"[^()]" ' Match any single character NOT present in the list “()”
"+" ' Between one and unlimited times, as many times as possible, giving back as needed (greedy)
您可以使用以下代码块提取所有匹配项
Try
Dim RegexObj As New Regex("[^()]+", RegexOptions.IgnoreCase)
Dim MatchResults As Match = RegexObj.Match(SubjectString)
While MatchResults.Success
' matched text: MatchResults.Value
' match start: MatchResults.Index
' match length: MatchResults.Length
MatchResults = MatchResults.NextMatch()
End While
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try
Dim input As String = "(3)_(9)--(11).(FT-2)"
Dim searchPattern As String = "\((?<keep>[^)]+)\)|(?<=\))(?<keep>[^()]+)"
Dim replacementPattern As String = "${keep}" + Environment.NewLine
Dim output As String = RegEx.Replace(input, searchPattern, replacementPattern)
Dim input = {"(3)_(9)--(11).(FT-2)", "(10)--(20).(10)/test--(99)"}
For Each s As String In input
Dim parts = Regex.Split(s, "\(|\)")
Console.WriteLine($"Input = {s}")
For Each p As String In parts
Console.WriteLine(p)
Next
Next
Console.ReadKey()
3条答案
按热度按时间6qfn3psc1#
一个选项是使用
[String]
的Split
方法另一个选项是匹配除
(
和)
之外的所有内容作为正则表达式,它将执行
[^()]+
故障
您可以使用以下代码块提取所有匹配项
6qqygrtg2#
这应该可行:
13z8s7eq3#
最简单的方法是使用
Regex.Split
(公式化为一个小控制台测试):基本上,我们有一行代码用于regex部分。
正则表达式
\(|\)
表示:在(
或)
处拆分,其中大括号使用\
转义,因为它们在正则表达式中具有特殊含义。稍短的正则表达式
[()]
(其中所需的字符包含在[]
中)将产生相同的结果。