正则expression式从数据中获取特定的string

我有以下数据

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 

从这个数据我想searchS amsung_Galaxy_Note_ 2并且重新调用其对应的192.168.56.102:5555

如何使用正则expression式

最简单的,你可以在多线模式下使用它:

 ^(\S+).*Samsung_Galaxy_Note_2 

并从组1中检索匹配。在正则expression式演示中 ,请参阅右窗格中的组捕获。

在JS中:

 var myregex = /^(\S+).*Samsung_Galaxy_Note_2/m; var matchArray = myregex.exec(yourString); if (matchArray != null) { thematch = matchArray[0]; } 

说明

  • ^锚主张我们在string的开头
  • (\S+)捕获任何不是空格字符的字符
  • .*匹配任何字符
  • Samsung_Galaxy_Note_2匹配文字字符

没有任何捕捉组织 ,通过积极向前看

 ^\S+(?=.*?Samsung_Galaxy_Note_2) 

DEMO