0

Instead of using mb_strlen and preg_match, I'm trying to devise a purely regex solution, so that I can shorten my code by piping everything through a function.

Minimum Possible Input (number chars are only used to demonstrate quantity)

  • 1@1234

Maximum Possible Input (number chars are only used to demonstrate quantity)

  • 123456789012345678901234567890123456789012345@1234
  • 1@123456789012345678901234567890123456789012345678
  • and everything in between like 123456790@123456789012345678901234567890123456789

The current pattern I've devised is ^.{1,}?@.{4,}?$, but I'm not sure how to limit the total characters to 50?

I've tried capturing and grouping everything (e.g. (^.{1,}?@.{4,}?$){,50}, [^.{1,}?@.{4,}?$]{,50}), but these obviously don't work.

oldboy
  • 5,729
  • 6
  • 38
  • 86

1 Answers1

3

You could try adding a negative lookahead at the start of the pattern which asserts that no more than 50 characters appear in total:

^(?!.{51,}).{1,}?@.{4,}?$

Demo

The negative lookahead (?!.{51,}) literally asserts that we don't see 51 or more characters, which then implies that there 50 or fewer characters present.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360