vb.Net - 如何修剪文本字符串?

时间:2016-01-18 00:50:56

标签: vb.net

这是我试图减少的文字串...... [是CMD的输出]

  

Microsoft Windows [Version 10.0.10586](c)2015 Microsoft Corporation。   保留所有权利。

     

C:\ Users \ User \ OneDrive \ Documents \ Visual Studio   2013 \项目\ TEST \ TEST \ BIN \调试和GT;! cd C:\ FTV \ ADB

     

C:\ FTV \ ADB> adb devices附加的设备列表   0715f7b5c1791d38设备

     

C:\ FTV \ ADB>退出

我只是希望得到0715f7b5c1791d38 device

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

您可以使用Regex来解决这个问题...您可以在此处查看:https://regex101.com/r/sM5hU4/1

 attached (.*?) device

以上

的细分
  • 第一个caturing组是(。*?),它匹配除换行符之外的任何字符......
  • 零和无限时间之间的量词(*?)仅在需要时扩展(懒惰)。

注意:如果在字符串中可能存在多次,则必须更改它,因为它需要捕获组...

答案 1 :(得分:-1)

Codexer在我写这篇文章时回答,他的回答解释了正则表达式,这是如何使用它:

    MsgBox(System.Text.RegularExpressions.Regex.Match("attached 0715f7b5c1791d38 device", "attached (.*?) device").Groups(1).Value)

使用您的字符串将“attached 0715f7b5c1791d38 device”更改为变量。

attached (.*?) device是正则表达式。

.Groups(0)将是完整的字符串

.Groups(1)将是第一个匹配组(您可以将所需的组放入括号中进行分组)

为了节省写作费用,您可以将Imports System.Text.RegularExpressions添加到课程顶部,然后您可以使用regex.match(expression, regex_string)代替System.Text.RegularExpressions

相关问题