0

I want to capture the strings from the following:

// want "Closed", "RequireApplication", "Open"
export type RegistrationMode = "Closed" | "RequireApplication" | "Open";

// want "All", "Comments", ..., "Url"
export type SearchType =
  | "All"
  | "Comments"
  | "Posts"
  | "Communities"
  | "Users"
  | "Url";

However, the matcher I wrote export type \w+ =(\S+)?(?:\s+\| (\S+))*; only matches the last group and misses the ones in the middle.

https://regex101.com/r/SBpaVY/1

What am I missing?

Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
  • Only the last overwriteing of a capture group is retained. If you quantify a capture group thats the typical behavior. However Dot-Net regex can do this with their _CaptureCollection_ capability. All others can't. – sln Jun 01 '23 at 21:46
  • If you want to chain matches to something, you can use [the `\G` anchor](https://www.regular-expressions.info/continue.html) which is supported by Java regex. Let's say you want to chain matches to `export type \w+` and capture what's inside the doublequotes you can use e.g. [`(?:\G(?!^)|^export type \w+)[|=\s]+\"([^\"]+)\"`](https://regex101.com/r/bNHgsD/1) The negative lookahead `(?!^)` after `\G` is just to prevent `\G` from matching at `^` start (default behaviour). – bobble bubble Jun 02 '23 at 08:29

0 Answers0