regex - 正则表达式从数据中获取特定字符串

标签 regex node.js

我有以下数据

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/

相关文章:

c# - 是否有专门用于 byte[] 数组的众所周知的 .NET REGEX 库?

python - 为什么正则表达式在 python 中返回错误?

node.js - 查询 "AND"Sqlite 3 REST

node.js - Express + Angular 路由导致无限循环+崩溃

node.js - 如何在 mongoose pre updateOne Hook 中获取文档 _id?

javascript - 使用 REST API 从本地主机进行开发时处理 CORS - Streamlabs API

javascript - 根据正则表达式搜索结果替换单词

regex - 如何在vim中重新编号列表?

javascript - REGEX - 正则表达式模式匹配

javascript - 将卡片从一堆移动到另一堆将项目从一个数组移动到另一个 javascript