1

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

0

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

6 Comments

one thing also if i assigned the word var device =Samsung_Galaxy_Note_2 then can i use ^\S+(?=.*?+device+)
sorry it is not working tried code ` var device = 'Samsung_Galaxy_Note_2'; //var device = 'Google_Nexus_7' var myregex = new RegExp("^(\S+).*" + device, 'm') var matchArray = myregex.exec(data); if (matchArray != null) { thematch = matchArray[0]; console.log(thematch) }` Am getting undefined while ouputing thematch
You mean the dynamically generated regex works for one but not the other? That doesn't sound right. In your tests, did you generate one regex for the Samsung, and one regex for the Google? And no typos?
dont know.....it is also not working..if putting device name directly it is working man!!!!but i need to put device itself...:-(
hi it worked when i use like this var myregex = new RegExp( '^(\\S+).*' + device, 'm')
|
1

Without any capturing groups and the one through positive lookahead,

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

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.