VB.NET匹配相同字符之间的字符串

时间:2012-08-27 15:26:38

标签: regex vb.net

我将不得不阅读一个正则表达式教程 - 到目前为止,我在这方面有一个心理障碍 - 现在我只是想尝试匹配以下模式:

ab234_12345_45678_afddsyyht788959

我正在尝试提取由下划线包围的第三个(可能是数字)项目(如果字符串具有错误的模式,当然返回无效格式)。

类似的东西:

Dim strOriginal As String = "ab234_12345_45678_afddsyyht788959"
Dim strFound As String
Try
    Dim matches As MatchCollection
    Dim regexStuff As New Regex("_.*?_")
    matches = regexStuff.Matches(strOriginal)
    strFound = matches.Item(0).Groups(2).Value.ToString
Catch
    strFound = "invalid"
End Try
MessageBox.Show(strFound)

问题在于模式 - 我无法找出任何有效的东西......(我也尝试过其他一些模式)

1 个答案:

答案 0 :(得分:1)

使用此正则表达式

[A-Za-z0-9]+_[A-Za-z0-9]+_([A-Za-z0-9]+)

匹配\1

如果您希望第三个组用([A-Za-z0-9]+)

替换(\d+)
相关问题