Reputation: 163
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"
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?
Upvotes: 1
Views: 60
Reputation: 784958
You may use this regex with a negated character class:
"account[^"\\]*\\*":\\*"([^"\\]*)
RegEx Breakup:
"account
: Match "account
[^"\\]*
: Match 0 or more of any char that is not "
and \
\\*":\\*"
: Match ":"
with optional \
s([^"\\]*)
: Our match, which is 0 or more of any char that is not "
and \
, captured in group #1Upvotes: 1
Reputation: 14900
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.
Upvotes: 1