匹配字符串中两个索引之间的子字符串

时间:2020-12-08 19:21:31

Hi guys I need a regex that doesn't match if in the first four chars there are the letters [aue] I' ve tried to use this regex [^aue]{4}. What am I doing wrong? I'm using vba Example: Match the String: xyzoa but not match axyzo because in the first four there is the letter a

嗨伙计我需要一个不匹配的正则表达式,如果在前四个字符中有字母[aue]我试图使用这个正则表达式[^ aue] {4}。我究竟做错了什么?我正在使用vba示例:匹配字符串:xyzoa但不匹配axyzo,因为在前四个中有字母a

1 个解决方案

#1


1  

You are not anchoring your regexp. So it can match anywhere in the string.

你没有锚定你的正则表达式。所以它可以匹配字符串中的任何位置。

This means that it doesn't match either xyzoa or azyxo. But it matches xyzo from xyzoa and zyxo from azyxo.

这意味着它与xyzoa或azyxo都不匹配。但它匹配来自xyzoa的xyzo和来自azyxo的zyxo。

To get around this you need to use ^ to indicate start of string;

要解决此问题,您需要使用^来表示字符串的开头;

^[^aue]{4}

Or if you the match to be the whole string;

或者如果你匹配成为整个字符串;

^[^aue]{4}.*$

#1


1  

You are not anchoring your regexp. So it can match anywhere in the string.

你没有锚定你的正则表达式。所以它可以匹配字符串中的任何位置。

This means that it doesn't match either xyzoa or azyxo. But it matches xyzo from xyzoa and zyxo from azyxo.

这意味着它与xyzoa或azyxo都不匹配。但它匹配来自xyzoa的xyzo和来自azyxo的zyxo。

To get around this you need to use ^ to indicate start of string;

要解决此问题,您需要使用^来表示字符串的开头;

^[^aue]{4}

Or if you the match to be the whole string;

或者如果你匹配成为整个字符串;

^[^aue]{4}.*$