0

I'm working in a project in Dart, and I need a RegEx to match every single character of the character set [a-zA-Z0-9] that is not inside single quotes, for example:

Hello my 'name' is

I want it to match "Hello my" and "is", but the RegEx needs to be versatile enough to encompass every situation instead of a specific pattern, the only thing I need to achieve is to match every character outside of single quotes.

  • Not sure about regex in Dart, if it is the same as JavaScript and it supports non-fixed width lookbehinds, [`(?<=^(?:[^']*'[^']*'[^']*)*)[^']+`](https://regex101.com/r/myqJ9A/1) could work. – Hao Wu Jul 21 '23 at 04:01
  • Your "Hello my" contains characters that are not in the range [a-zA-Z0-9]. I mean `space` character – mezoni Aug 03 '23 at 17:14

1 Answers1

1

You can use allMatches to find all non-' characters ([^']) that are not in a quote ('[^']*').

RegExp r = RegExp(r"([^']*?)($| *'[^']*' *)");
print(r.allMatches("Hello my 'name' is").map((m) => m.group(1)));

Results in "Hello my" and "is" as in your example.

Etienne Laurin
  • 6,731
  • 2
  • 27
  • 31
  • Ohh and by the way, I forgot to specify this in the answer, let's suppose I don't want it to match "-" or "." but only alphanumeric characters, how would I go about chaging the RegEx? – fueripe-desu Jul 21 '23 at 03:53
  • 1
    I wouldn't recommend incorporating that into one regex, it'll be even more unmaintainable. You can match `[a-zA-Z0-9]` on the results of the a regex that removes quoted strings. – Etienne Laurin Jul 21 '23 at 03:57