2

I need regex that match Scheme identifier that will terminate if it find any of the not allowed strings.

I have code like this:

function make_tokens_re() {
    var tokens = specials.names()
        .sort((a, b) => b.length - a.length || a.localeCompare(b))
        .map(escape_regex).join('|');
    return new RegExp(`(#\\\\(?:x[0-9a-f]+|${character_symbols}|[\\s\\S])|#f|#t|#;|(?:${num_stre})(?=$|[\\n\\s()[\\]])|\\[|\\]|\\(|\\)|\\|[^|]+\\||;.*|(?:#[ei])?${float_stre}(?=$|[\\n\\s()[\\]])|\\n|\\.{2,}|(?!#:|'#[ft])(?:${tokens})|[^(\\s)[\\]]+)`, 'gim');
}

NOTE: This regex is used in String::split.

What I need to change [^(\\s)[\\]]+ to also don't match tokens, special character list (default ` ' , ,@ there can be more and longer, they can be added by the user) they should be acting as separators and end the symbol.

I've tried this:

/.+(?!\)|\(|\[|\]|`|'|,@|,)/

but it match xxxx,, I think what I need is and operator not or.

I've also tried this:

/.*(?!,).(?!,@)./

but when tweaking it only work with single string either ,@ or ,.

Is something like this possible with Regular expressions?

EDIT:

This almost works:

/.*(?=,@|,)/

the problem is when I'm adding or |$ it match including the ,@ or ,.

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • Hey, your regex is very complex, but I would try doing something like NOT(a OR b OR c) `?!( | | | | )`. – Deykun Aug 08 '20 at 21:43
  • @Deykun you mean `.*(?!(?:,@|,))`? It don't work. PS: My regex is just `[^(\\s)[\\]]+` that also need exception in form of strings. I've added my whole code for context. – jcubic Aug 08 '20 at 21:46

1 Answers1

1

The solution was two values:

`[^(\\s)[\\]]+(?=${tokens})|[^(\\s)[\\]]+`

first will match any character that have my tokens

So general solution look like this, if you have list foo, bar, baz:

use:

/\w+(?=foo|bar|baz)|\w+/

it will match any word that in next token have any provided value or just the word without anything.

jcubic
  • 61,973
  • 54
  • 229
  • 402