我有以下数据
List of devices attached
192.168.56.101:5555 device product:vbox86p model:Samsung_Galaxy_Note_2___4_3___API_18___720x1280 device:vbox86p
192.168.56.102:5555 device product:vbox86tp model:Google_Nexus_7___4_3___API_18___800x1280 device:vbox86tp
我想从这些数据中搜索 Samsung_Galaxy_Note_
2 并返回其对应的 192.168.56.102:5555
如何使用正则表达式
最佳答案
最简单的是,您可以在多行模式下使用它:
^(\S+).*Samsung_Galaxy_Note_2
并从第 1 组检索匹配项。在 the regex demo 中,在右 Pane 中查看组捕获。
在 JS 中:
var myregex = /^(\S+).*Samsung_Galaxy_Note_2/m;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
thematch = matchArray[0];
}
说明
^
anchor 断言我们位于字符串的开头(\S+)
将所有非空白字符捕获到组 1.*
匹配任何字符Samsung_Galaxy_Note_2
匹配文字字符
关于regex - 正则表达式从数据中获取特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24886552/