-1

I'm creating a search system, and want to support a mix of input terms, for example:

@bob "xxx",'yyy', "freak'd out" 'bob two' "" can I #handle/children #nope @ok(42)

I'd like a regex to grab all the insides of balanced quotes (not tripping over the

To make matters worse, I want to support common search operators that can be prefixed to words or quotes phrases, like so:

+@bob "xxx",'yyy', "freak'd out" -'bob two' "" can I #handle/children -#nope @ok(42)

and in the case of -'bob two' I want to land up with -bob two.
I also want to ignore the empty "".

JGC
  • 505
  • 1
  • 4
  • 12
  • Maybe something simple like [`[+-]?(?:"[^"]*"|'[^']*'|[@#]?[^"'#@+-]+)`](https://regex101.com/r/22ksym/1) would suffice but I doubt it :p You'd need to do further parsing with this one, is just to split into desired parts... – bobble bubble Jul 23 '22 at 21:57

1 Answers1

1

I found this answer to a simpler version of this question and built on it, landing up with:

/\s([\-\+\!]?)([\-\+\!]?)(?:([\'"])(.+?)\3)|([\-\+\!]?[\w\-#@\/\(\)]+)/g

You can try it out here: https://regex101.com/r/8xxydh/1

This needs some post-processing in order to get out what I need, but it's simply concatenating match groups

  • 1 (optional operator prefix)
  • 4 (phrase inside quotes)
  • 5 (word not in quotes)

(You won't get both 4 and 5 in a result.)

Doing this I get the following list:

  1. +@bob
  2. xxx
  3. yyy
  4. freak'd out
  5. -bob two'
  6. can
  7. I
  8. #handle/children
  9. -#nope
  10. @ok(42)
JGC
  • 505
  • 1
  • 4
  • 12