0

This is my RegEx:

"^[^\.]([\w-\!\#\$\%\&\'\*\+\-\/\=\`\{\|\}\~\?\^]+)([\.]{0,1})([\w-\!\#\$\%\&\'\*\+\-\/\=\`\{\|\}\~\?\^]+)[^\.]@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$"

I need to match only strings less than 255 characters.

I've tried adding the word boundaries at the start of the RegEx but it fails:

"^(?=.{1,254})[^\.]([\w-\!\#\$\%\&\'\*\+\-\/\=\`\{\|\}\~\?\^]+)([\.]{0,1})([\w-\!\#\$\%\&\'\*\+\-\/\=\`\{\|\}\~\?\^]+)[^\.]@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$"
dda
  • 6,030
  • 2
  • 25
  • 34
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

3 Answers3

4

You need the $ in the lookahead to make sure it's only up to 254. Otherwise, the lookahead will match even when there are more than 254.

(?=.{1,254}$)

Also, keep in mind that you can greatly simplify your regex because many characters that would usually need to be escaped do not need to when in a character class (square brackets).

"[\w-\!\#\$\%\&\'\*\+\-\/\=\`\{\|\}\~\?\^]"

is the same as this:

"[-\w!#$%&'*+/=`{|}~?^]"

Note that the dash must be first in the character class to be a literal dash, and the caret must not be first.

With some other simplifications, here is the complete string:

"^(?=.{1,254}$)[-\w!#$%&'*+/=`{|}~?^]+(\.[-\w!#$%&'*+/=`{|}~?^]+)*@((\d{1,3}\.){3}\d{1,3}|([-\w]+\.)+[a-zA-Z]{2,6})$"

Notes:

  • I removed the stipulation that the first char shouldn't be a period ([^.]) because the next character class doesn't match a period anyway, so it's redundant.
  • I removed many extraneous parens
  • I replaced [0-9] with \d
  • I replaced {0,1} with the shorthand "?"
  • After the @ sign, it seemed that you were trying to match an IP address or text domain name, so I separated them more so it couldn't be a combination
  • I'm not sure what the optional square bracket at the end was for, so I removed it: "(]?)"

I tried it in Regex Hero, and it works. See if it works for you.

Brian Stephens
  • 5,161
  • 19
  • 25
  • I've tried your last suggestion but it not works for me, I copy/paste your suggestion and replaced that part in my RegEx to somplify it but then the matched string is not matched anymore, maybe can you update you answer with my regex modified using your simplified suggestion? PS: Sorry for my english – ElektroStudios May 01 '13 at 16:27
  • `$` for end is right ✅ – xgqfrms Apr 24 '23 at 17:57
2

This depends on what language you are working in. In Python for example you can regex to split a text into separate strings, and then use len() to remove strings longer than the 255 characters you want

rcbevans
  • 7,101
  • 4
  • 30
  • 46
1

I think this post will help. It shows how to limit certain patterns but I am not sure how you would add it to the entire regex.

Community
  • 1
  • 1
jaredsmith
  • 7,306
  • 3
  • 20
  • 31