Regex:匹配除“

时间:2022-03-20 20:13:34

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

我试图实现lexer并尝试创建regex,它将匹配任何内容,但不匹配以下内容:

  • <
  • <
  • {{
  • { {
  • {%
  • { %

There is that i'm trying:

我正在尝试:

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

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

但它也不关注ant的单个“{”和“%”符号。

It it possible to do with regex?

可以用regex吗?

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

输入:“foo {bar < baz{%”输出:“foo”,“bar”,“baz”

2 个解决方案

#1


2  

You can use lookaround based regex:

你可以使用lookaround的regex:

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

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

(?!{[{%])为负值,以匹配非{{或{%}的非空格文本。

RegEx Demo

RegEx演示

#2


1  

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: (<|{{|{%)

我认为你正在编写模板语言你可能想要对这些字符进行分割,对吧?如果是,那么您将在正的regex:(<|{|{%)上进行分割

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

使用http://www.regexr.com/了解关于regex的更多信息。

#1


2  

You can use lookaround based regex:

你可以使用lookaround的regex:

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

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

(?!{[{%])为负值,以匹配非{{或{%}的非空格文本。

RegEx Demo

RegEx演示

#2


1  

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: (<|{{|{%)

我认为你正在编写模板语言你可能想要对这些字符进行分割,对吧?如果是,那么您将在正的regex:(<|{|{%)上进行分割

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

使用http://www.regexr.com/了解关于regex的更多信息。