Psl
Psl

Reputation: 3920

regular expression to get a particular string from a data

I have following data

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

from this data i want to search Samsung_Galaxy_Note_2 and retunn its corresponding 192.168.56.102:5555

how it is possbile using regular expressions

Upvotes: 1

Views: 61

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174786

Without any capturing groups and the one through positive lookahead,

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

DEMO

Upvotes: 1

zx81
zx81

Reputation: 41838

At the simplest, you can use this in multi-line mode:

^(\S+).*Samsung_Galaxy_Note_2

and retrieve the match from Group 1. In the regex demo, see the group capture in the right pane.

In JS:

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

Explanation

  • The ^ anchor asserts that we are at the beginning of the string
  • (\S+) captures to Group 1 any chars that are not white-space chars
  • .* matches any chars
  • Samsung_Galaxy_Note_2 matches literal chars

Upvotes: 2

Related Questions