Reputation: 5783
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?
Upvotes: 2
Views: 93
Reputation: 1480
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.
Upvotes: 0
Reputation: 39893
" 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.
Upvotes: 2
Reputation: 57470
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}).*
Upvotes: 1
Reputation: 301117
Try this:
^(123|321)(?!\s{6}).*
(uses a negative lookahead so see if there are 6 whitespaces in .*
match)
Upvotes: 1