开发者

How do I write a regex that won't match a certain amount of whitespace?

开发者 https://www.devze.com 2023-04-12 12:28 出处:网络
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'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?


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


Try this:

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

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


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}).*


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号