如何编写不匹配一定量空格的正则表达式?

发布于 2024-12-09 01:32:35 字数 485 浏览 0 评论 0原文

我正在尝试编写一个不会匹配一定数量空格的正则表达式,但它并没有按照我预期的方式进行。

我有这些字符串:

123      99999 # has 6 white spaces
321      99999 # same
123   8888    # has 3 white spaces  \
321   8888    # same                | - These are the lines I
1237777                             |   want to match
3217777                             /

我想匹配最后四行,即以 123 或 321 开头,后跟除 6 个空白字符之外的任何字符:

^(123|321)[^\ ]{6}.*

这似乎不起作用 - 这只匹配最后两行。我缺少什么?

I'm trying to write a regex that won't match a certain number of white spaces, but it's not going the way I expected.

I have these strings:

123      99999 # has 6 white spaces
321      99999 # same
123   8888    # has 3 white spaces  \
321   8888    # same                | - These are the lines I
1237777                             |   want to match
3217777                             /

I want to match the last four lines, i.e. starts with 123 or 321 followed by anything but 6 whitespace characters:

^(123|321)[^\ ]{6}.*

This doesn't seem to do the trick - this matches only the two last ones. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一杯敬自由 2024-12-16 01:32:35
"   888"

如果你匹配这个,这与 [^\ ]{6} 不匹配:这就是说

[not a space][not a space][not a space][not a space][not a space][not a space]

在这种情况下,你遇到的问题是前 3 个字符是空格,所以它不匹配正确的。

您可以使用负前瞻 ^(123)|(321)(?!\s{6})。我更喜欢的是,因为它更具可读性,是编写正则表达式来匹配您不想要的内容,然后求反(即 not! 等) 。我对你的数据了解不够,但我会使用 \s{6},然后否定它。

"   888"

If you match this up, this does not match [^\ ]{6}: this is saying

[not a space][not a space][not a space][not a space][not a space][not a space]

In this case, you have the problem that the first 3 characters are a space, so it's not matching up right.

You can use a negative lookahead ^(123)|(321)(?!\s{6}). What I prefer because it is more readable, is to write the regular expression to match what you don't want, then negate (i.e., not, !, etc.). I don't know enough about your data, but I would do use \s{6}, then negate it.

萌化 2024-12-16 01:32:35

试试这个:(

^(123|321)(?!\s{6}).*

使用负向前看,看看 .* 匹配中是否有 6 个空格)

Try this:

^(123|321)(?!\s{6}).*

(uses a negative lookahead so see if there are 6 whitespaces in .* match)

睫毛上残留的泪 2024-12-16 01:32:35

你用什么语言做这个?如果在 Perl 或支持 PCRE 的语言中,您可以简单地使用否定前瞻断言:

^(123)|(321)(?!\ {6}).*

What language are you doing this in? If in Perl or something that supports PCREs, you can simply use a negative lookahead assertion:

^(123)|(321)(?!\ {6}).*
只是偏爱你 2024-12-16 01:32:35

您需要首先声明它可能有 3 个空格,然后否认另外 3 个空格的存在,如下所示:

^([0-9]+)(\s{0,3})([^ ]{3})([0-9]*)$

^([0-9]+) = 接受字符串开头的一个或多个数字。

(\s{0,3}) = 接受零个或最多三个空格。

([^ ]{3}) = 禁止在允许的空格后添加接下来的 3 个空格。

([0-9]*) = 接受空格后面的任何数字,直到字符串末尾。

或者:

^([0-9]+)(\s{0,3})(?!\s+)([0-9]*)$

这里唯一的变化是,在三个允许的空格之后,它将不再接受任何空格(我特别喜欢第二个选项,因为它更具可读性)。

希望有帮助。

You need to first say that it may have 3 whitespaces and then deny the existence of the three more whitespaces, like this:

^([0-9]+)(\s{0,3})([^ ]{3})([0-9]*)$

^([0-9]+) = Accepts one or more numbers in the beginning of your string.

(\s{0,3}) = Accepts zero or up to three spaces.

([^ ]{3}) = Disallow the next 3 spaces after the allowed spaces.

([0-9]*) = Accepts any number after spaces till the end of your string.

Or:

^([0-9]+)(\s{0,3})(?!\s+)([0-9]*)$

The only change here is that after the three allowed spaces it won't accept any more spaces (I particularly like this second option more because it's more readable).

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文