DEV Community

How I can extract words from strings using regular expressions?

Prathamesh More on November 24, 2020

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": "...
Collapse
 
qm3ster profile image
Mihail Malo • Edited

That looks like tokenisation, except It's strange that the key-value pairs are one token.
I'd more expect a result like

[
  '(',
  '"type"',
  ':',
  '"Person"',
  'AND',
  '(',
  '"specialFields.email"',
  ':',
  '"prathameshmore@gmail"',
  'OR',
  '"specialFields.address"',
  ':',
  '"Jaysingpur"',
  ')',
  ')'
]
Enter fullscreen mode Exit fullscreen mode

However if the grammar is really simple you might just get away with parsing it in one pass to

{
  _t: "AND",
  members: [
    {_t: "EQ", k: "type", v: "Person"},
    {
      _t: "OR",
      members: [
        {_t: "EQ", k: "specialFields.email", v: "prathameshmore@gmail"},
        {_t: "EQ", k: "specialFields.address", v: "Jaysingpur"}
      ]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

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 keyords AND&OR, and escaped "\"" (or however else double quote character is escaped in your query format)

Collapse
 
pprathameshmore profile image
Prathamesh More

("\w+.?\w*":"\w+@?.?") This is working now.

How I can get the bracket and AND and `OR'?

Collapse
 
pprathameshmore profile image
Prathamesh More

I wanted to use Stack to create a mongoose filter.

Collapse
 
qm3ster profile image
Mihail Malo
  1. I updated my post, you were just too quick to reply :D
  2. What is "Stack"?
Thread Thread
 
pprathameshmore profile image
Prathamesh More • Edited

I am trying to create this output

{
 "$and": [
    { "type": "Nanoheal" },
    {
      "$or": [
        { "specialFields.address": "Jaysingpur" },
        { "specialFields.email": "prathameshmore@gmail.com" }
      ]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

It's working when I am using a string like this

 let operands = query.split("'").filter(Boolean);

//Output

 [
  '(',
  '"type": "Nanoheal"',
  'AND',
  '(',
  '"specialFields.email": "prathameshmore@nanoheal"',
  'OR',
  '"specialFields.address": "Jaysingpur"',
  ')',
  ')'
]

Enter fullscreen mode Exit fullscreen mode
const query = `'('"type": "Nanoheal"'AND'('"specialFields.email": "prathameshmore@nanoheal"'OR'"specialFields.address": "Jaysingpur"')'')'`;
Enter fullscreen mode Exit fullscreen mode

I want to make the query simple from the user side.

("type": "Person" AND ("specialFields.email": "prathameshmore@gmail.com" OR "specialFields.address": "Jaysingpur"))

Thread Thread
 
pprathameshmore profile image
Prathamesh More

The stack is used to create this object

{
 "$and": [
    { "type": "Nanoheal" },
    {
      "$or": [
        { "specialFields.address": "Jaysingpur" },
        { "specialFields.email": "prathameshmore@gmail.com" }
      ]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
stereobooster profile image
stereobooster

Use parser, for example pegjs.org/online

Expression
  = "(" _ head:(Expression / Pair) _ ")" {
      return head;
    } /
    "(" _ head:(Expression / Pair) _ operator:("AND" / "OR") _ tail: (Expression / Pair) _ ")" {
      return { [operator]: [head, tail]};
    }

Pair
  = head:String _ ":" _ tail:String {
      return [head, tail];
    }

String "string"
  = "\"" [^\"]+ "\"" { return text(); }

_ "whitespace"
  = [ \t\n\r]*
Enter fullscreen mode Exit fullscreen mode