I have searched on the internet but I don't get any solution. I just confused.
I have this string
("type": "Person" AND ("specialFields.email": "prathameshmore@gmail.com" OR "specialFields.address": "Jaysingpur"))
How can I extract data from like this
[
'(',
'"type": "Person"',
'AND',
'(',
'"specialFields.email": "prathameshmore@gmail"',
'OR',
'"specialFields.address": "Jaysingpur"',
')',
')'
]
Please anyone can help with it.
Top comments (19)
That looks like tokenisation, except It's strange that the key-value pairs are one token.
I'd more expect a result like
However if the grammar is really simple you might just get away with parsing it in one pass to
However, in any case, RegEx can only be used to parse small parts of this, if at all.
Especially, you must consider key and value strings that contain the characters
[](),:
, the keyordsAND
&OR
, and escaped"\""
(or however else double quote character is escaped in your query format)("\w+.?\w*":"\w+@?.?") This is working now.
How I can get the bracket and
AND
and `OR'?I wanted to use Stack to create a mongoose filter.
I am trying to create this output
It's working when I am using a string like this
I want to make the query simple from the user side.
("type": "Person" AND ("specialFields.email": "prathameshmore@gmail.com" OR "specialFields.address": "Jaysingpur"))
The stack is used to create this object
Any idea?
[
'(',
'"type": "Person"',
'AND',
'(',
'"specialFields.email": "prathameshmore@gmail"',
'OR',
'"specialFields.address": "Jaysingpur"',
')',
')'
]
One possible RegEx that creates these matches is
try it out here
However, regex is often brittle for parsing, and produces uninformative errors (or worse, silently skips/corrupts data)
Let me check. Thank man
Thanks, man. It works. You saved my job.
What language are you using? Javascript?
Are you on the NodeJS runtime?
Can you use libraries from npm?
Yes, I am using JS and Node.js.
Yes, I can use lib from npm. Can you suggest to me?
I am a junior developer. Joined 1 month ago. You saved me.
Thanks, man.
Look output now
Filter is final output
Nah, you're good.
But for correct treatment of strings, such as if
"ty\"pe": "Pe\"rson"
is allowed, you should look at using a proper tokenizer, for examplemoo
: github.com/no-context/moo#states (see how they match string escape here!)You can then take tokens from this tokenizer yourself, or give it to for example
nearley
: nearley.js.org/docs/tokenizersI suggest you read the documentation for these two libraries later today when you have time, you will then be a head above most people in tasks where custom text formats need to be parsed.
Thanks Mihail for such valuable time.
How I can modify the Regex so can support for
>
,<
,<=
,>=
,=
and!=
.E.g.
This is working
Use parser, for example pegjs.org/online