linqpad中的以下vb语句
Dim userAgent As String = "Mozilla/5.0 (Windows NT 6.1; WOW64)"
Dim pattern As String = "\(([^\]]+)\;"
Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(pattern)
Dim operatingSystem As Match = regex.Match(userAgent)
operatingSystem.Value.Dump()
输出为(Windows NT 6.1;
这个正则表达式是否有办法从结果匹配中省略(
和;
?
答案 0 :(得分:1)
答案 1 :(得分:1)
只需访问第一个捕获的组:
operatingSystem.Groups(1).Value
您可以按照以下方式改进模式:
Dim pattern As String = "\(([^;]+)"
答案 2 :(得分:0)
如果您不需要匹配,请使用环视,但只检查以下内容:
Dim userAgent As String = "Mozilla/5.0 (Windows NT 6.1; WOW64)"
Dim pattern As String = "(?<=\()[^\]]+(?=;)"
Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(pattern)
Dim operatingSystem As Match = regex.Match(userAgent)
Dim resulting_value As String = operatingSystem.Value
REGEX EXPANATION :
(?<=\()
- 在...之前检查(
的正面观察...... [^\]]+
- 除]
... (?=;)
- 后跟;
。注意在.NET中,你甚至不必在字符类的最后位置转义]
,所以即使(?<=\()[^]]+(?=;)
也会这样做。