2

I want to capture BBKH2-0272-2 , BBH@5 , 1234 in the below string (capture value where key is ssidname or SSIDName).

{"SSIDIndex":2,"SSIDName":"BBKH2-0272-2" ,"Band":"2.4GHz","Status":"RadioIndex":1,"SSIDIndex":3,"SSIDName":"BBH@5","ssidname":"1234", "ssid_name":"MKLM-098", ssidname=ppoin }

I am using this regex :

(?<=(?i)("SSIDName":"))(\w{1,8})

but it's not working. Thanks in advance.

Mohit Agrawal
  • 323
  • 4
  • 13

5 Answers5

1

You don't need look behind, and can use this regex and capture group 1,

(?i)"SSIDName":"([^"]+)"

Basically you want to capture all the values of key attribute SSIDName and case insensitive as one of your keys is in lowercase.

Live Demo

Your regex, (?<=(?i)("SSIDName":"))(\w{1,8}) is fine but will obstruct while capturing BBH@5 as you are trying to capture it with \w and @ is not part of \w. If you change your regex to this,

(?<=(?i)(?:"SSIDName":"))([\w@-]+)

Then even your regex can capture what you need.

Demo for your modified regex

But in general, you should try and keep your regex to be simple and avoid look arounds as not all languages and tools support look arounds, and your regex can work for most languages/tools.

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • You should add `-` in your second regex otherwise it will not select the name completely if there is a `-` in it. Also note that: https://stackoverflow.com/questions/4919889/is-there-a-standard-that-defines-what-is-a-valid-ssid-and-password SSIDName can accept any characters so your `[^"]` is a better solution anyway +1! – Allan Dec 12 '18 at 07:11
  • @Allan: Yes Alan you are right. `-` will also be needed. But best is to use my solution which doesn't use look arounds at all and captures any text contained within doublequotes. – Pushpesh Kumar Rajwanshi Dec 12 '18 at 07:13
  • 1
    Thanks a lot for regex and explanation @pushpesh . It's working . – Mohit Agrawal Dec 12 '18 at 07:13
  • Glad to help Mohit :) – Pushpesh Kumar Rajwanshi Dec 12 '18 at 07:14
1

Use the following non-greedy approach:

(?i)(?:"ssidname":")(.*?)"

All I am doing is looking for the string "ssidname":" and capturing the value (without quotes) until the end quote is encountered.

Demo

CinCout
  • 9,486
  • 12
  • 49
  • 67
1

Try this one too.

 (?<=(?i)("SSIDName":"))([a-zA-z0-9-@]+)
Prashant Deshmukh.....
  • 2,244
  • 1
  • 9
  • 11
1

I don't know why you have been using such difficult regex

this can be done by using

regex : /^"SSIDName":"(.*?)"/gmi

g - checks globally ie. don't return after the first match.

m - matches from start to end of line ie. multiline.

i - case-insensitive match.

Check Demo here.

vikram sahu
  • 161
  • 12
0

You can also try the following regex (very close to yours):

(?i)(?<="SSIDName":")[^"]+(?=")

with the string:

{"SSIDIndex":2,"SSIDName":"BBKH2-0272-2" ,"Band":"2.4GHz","Status":"RadioIndex":1,"SSIDIndex":3,"SSIDName":"BBH@5","ssidname":"1234" }

It will only select:

BBKH2-0272-2
BBH@5
1234

As shown in https://regex101.com/r/3Teuiq/1/

Allan
  • 12,117
  • 3
  • 27
  • 51