Anton Medvedev
Anton Medvedev

Reputation: 3668

Regex: match anything but not "<", "{{" and "{%"

I trying to implement lexer and trying to create regex what will match anything but not the following:

There is that i'm trying:

[^(<|{{|{%)]+

But it also does not watch ant single "{" and "%" symbols.

It it possible to do with regex?

Input: "foo {{ bar < baz {%" Output: "foo ", " bar ", "baz "

Upvotes: 2

Views: 207

Answers (2)

anubhava
anubhava

Reputation: 786261

You can use lookaround based regex:

(?<=\s|^)(?!{[{%]|<)\S+

(?!{[{%]) is negative lookahead to match any non-space text that is not {{ or {%.

RegEx Demo

Upvotes: 2

mevdschee
mevdschee

Reputation: 1714

I think you are writing a templating language and you may want to split on these characters, right? If so, then you split on the positive regex: (<|{{|{%)

Use http://www.regexr.com/ to learn more about regexes.

Upvotes: 1

Related Questions