jcubic
jcubic

Reputation: 66660

Regex that match everything except the list of strings

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 ,.

Upvotes: 2

Views: 975

Answers (1)

jcubic
jcubic

Reputation: 66660

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.

Upvotes: 1

Related Questions