Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regular expression to match first element

My data strings can be in this format

1.

{"name":"Lokesh","accountNumber":"3044444444","city":"New York"}
"{\"name\":\"Lokesh\",\"accountNumber\":\"3044444444\",\"city\":\"New York\"}"
"\"{\\\"name\\\":\\\"Lokesh\\\",\\\"accountNumber\\\":\\\"3044444444\\\",\\\"city\\\":\\\"New York\\\"}\""

Basically, a JSON object that can be stringified any number of times or it can be similar looking string for example
"hello"="world"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have written regex as

/\\*".*account.*\\*":\\*"(.*?)\\*".*/g

But it matches New YorK

But I want to match the first element i.e. 3044444444. How can I achieve this?

>Solution :

Use: \\*".*account[^,]*\\*":\\*"((.*?)\\*)".*
see: https://regex101.com/r/vb2rx4/1

Then only thing changed is that i replaced . by [^,]. A . will match anything, and [^,] will match anything but a comma.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading