Regex that match everything except the list of strings

萝らか妹 提交于 2021-01-27 19:33:10

问题


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


回答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.



来源:https://stackoverflow.com/questions/63320400/regex-that-match-everything-except-the-list-of-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!