Alex
Alex

Reputation: 23

RegEx - allow whitespace at start and end of the string with fixed length

Currently I am working on a regex with the following conditions:

I have created below regex but somehow length validation is not working as expected.

(^\s*?)((?!(-))([0-9-]*)){8,10}(\s*?)$ https://regex101.com/r/aJNmqj/1

Upvotes: 2

Views: 109

Answers (3)

The fourth bird
The fourth bird

Reputation: 163477

Your pattern matches too much because it first matches optional leading whitespace chars.

But when it gets to the first digit in the example string, in this part ((?!(-))([0-9-]*)){8,10} the assertion (?!(-) is true and then this part [0-9-]* matches to the last digit in the example string.

It can then fulfill repeating the quantifier {8,10} as it already is at the position after the last digit in the example string where there is no - following and it can match optional digits.


You could start the pattern with optional whitespace chars followed by a digit, and then match 7-9 digits or hyphens:

^\s*[0-9][0-9-]{7,9}\s*$

See a regex101 demo.

Upvotes: 1

Peter Thoeny
Peter Thoeny

Reputation: 7616

From your original regex fragment [0-9-]* it looks like you want to allow only digits and dashes. Here is a solution that:

  • allows 8-10 digits or dashes
  • no dash in the beginning
  • may have leading and trailing spaces

Code with test cases:

const regex = /^(?!\s*-)\s*[0-9-]{8,10}\s*$/;
[
  '1234567',
  ' 123 456 7 ',
  '12345678',
  '1234567-',
  '123-5678',
  '-2345678',
  ' 12345678 ',
  ' 123 456 78 ',
  '12345678',
  '12345678 ',
  '12 34 56 78 ',
  '123456789',
  ' 123456789',
  ' 12 34 56 78 9',
  '1234567890',
  ' 123 456 7890',
  '12345678901'
].forEach(str => {
  let ok = regex.test(str);
  console.log('"' + str + '" ==> ' + ok);
});

Explanation of regex:

  • ^ -- anchor at start of string
  • (?!\s*-) -- negative lookahead for optional spaces and dash
  • \s* -- optional spaces
  • [0-9-]{8,10} -- 8 to 10 digits/dashes
  • \s* -- optional spaces
  • $ -- anchor at end of string

Learn more about regex: https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex

Upvotes: 1

Slbox
Slbox

Reputation: 13138

You should be able to achieve this with a simple regex like this: ^(\s*?)[0-9-]{8,10}(\s*?)$

I believe your other one didn't work because negative lookaheads can't be operated on by the quantifier {8,10} Fortunately, the negative lookahead isn't actually necessary.

Your regex got convoluted while you were trying to do this I bet, because it's happened to me too.

Upvotes: 1

Related Questions