1

Example word: name.surname@exm.gov.xx.en

I want to limit the name + surname's total length to 12.

Ex: If name's length is 5 then the surname's length cannot bigger than 7.

My regex is here: ([a-z|çöşiğü]{0,12}.[a-z|çöşiğü]{0,12}){0,12}@exm.gov.xx.en

Thx in advance

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

1

If there should be a single dot present which should not be at the start or right before the @, you could assert 13 characters followed by an @

^(?=[a-zçöşğü.]{13}@)[a-zçöşğü]+\.[a-zçöşğü]+@exm\.gov\.xx\.en$

In parts

  • ^ Start of string
  • (?= Positive lookahead, assert what is on the right is
    • [a-zçöşğü.]{13}@ Match 13 times any of the listed followed by an @
  • ) Close lookahead
  • [a-zçöşğü]+\.[a-zçöşğü]+ Match 2 times any of the listed with a dot inbetween
  • @exm\.gov\.xx\.en Match @exm.gov.xx.en
  • $ End of string

Regex demo

Note that I have omitted the pipe | from the character class as it would match it literally instead of meaning OR. If you meant to use it as a char, you could add it back. I also have remove the i as that will be matched by a-z

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Name and surname are required and also a dot between them is required too, thx for your answer but your regex gave me a limitless query – Uzay Yiğit GÖK May 15 '20 at 19:16
  • What do you mean by `gave me a limitless query`? – The fourth bird May 15 '20 at 19:19
  • An 11-character shorter variant of #1: `^(?=[^@]{3,12}@exm\.gov\.xx\.en$)[a-zçöşiğü]+\.[a-zçöşiğü]+`. [Demo](https://regex101.com/r/ZQNtpR/2/). Oh, wait. I thought this was [Code Golf](https://codegolf.stackexchange.com/). – Cary Swoveland May 15 '20 at 19:57
  • 1
    @CarySwoveland You can make it even 2 characters shorter removing the `i` from the character classes. Lets hope matching the second part of the email address is not required :-) – The fourth bird May 15 '20 at 20:24
  • 1
    If vb.net's regex engines supports Posix with `\p{L}` matching a Unicode letter and it is acceptable to match any Unicode letter that matches `\p{L}`, we can shave off another 23 characters with `^(?=[^@]{3,12}@exm\.gov\.xx\.en$)\p{L}+\.\p{L}+`. [Demo](https://regex101.com/r/ZQNtpR/4/). Hmmm, `\p{L}` doesn't work at a .net regex tester I found. Nor does `[[:alpha:]]`. Curiously, nor does `[[:alpha:]]` work with PCRE at regex101.com. (Maybe it needs to be represented differently.) – Cary Swoveland May 15 '20 at 21:23
  • @CarySwoveland It works in the .NET regex tester, but you could add `\r?` before the `$` like `^(?=[^@]{3,12}@exm\.gov\.xx\.en\r?$)\p{L}+\.\p{L}+` [demo](http://regexstorm.net/tester?p=%5e%28%3f%3d%5b%5e%40%5d%7b3%2c12%7d%40exm%5c.gov%5c.xx%5c.en%5cr%3f%24%29%5cp%7bL%7d%2b%5c.%5cp%7bL%7d%2b&i=name.surname%40exm.gov.xx.en%0d%0apolo.polopony%40exm.gov.xx.en%0d%0a%c3%a7%c3%b6%c5%9f.i%c4%9f%c3%bc%40exm.gov.xx.en%0d%0a&o=m) It is explained by Wiktor in this excellent post https://stackoverflow.com/questions/40058265/net-regex-matching-with-the-end-of-the-string-and-not-of-line-even-with-mult – The fourth bird May 15 '20 at 21:28
  • 1
    Thanks, but it pains me to give up three characters. Oh, well, life goes on. – Cary Swoveland May 15 '20 at 21:52
  • I mean it allows me to write longer than 12 characters. name is required. surname is required. A dot between first and last name is required. And the last part '@exm.gov.xx.en' is required. Any other mails are not allowed, total length of name and surname cannot longer than 12. If name (5 char) than surname(7char) If name(1char) than surname(11char). I need something like that: name.length=x, surname.length=12-x – Uzay Yiğit GÖK May 15 '20 at 21:56
  • @yigit.gok Then you can assert 13 chars and match the 2 parts with a dot inbetween. I have update the answer. – The fourth bird May 15 '20 at 22:04
  • Why in the world would you want to use a method that requites all this discussion when the one liner in comments by @AndrewMorton will do the trick? What a nightmare! – Mary May 16 '20 at 01:24
  • @Mary Splitting on @ does not take all the requirements into account. – The fourth bird May 16 '20 at 14:32
  • The only requirement I see is "limit the name + surname's total length to 12". – Mary May 16 '20 at 21:11
  • If the requirements changed in comments, it should be added to the question. – Mary May 17 '20 at 01:27
  • @Mary Please read the question, the requirements did not change. Splitting on an @ will not take into account that the part after the @ should be `exm.gov.xx.en` as you can see in the pattern that the OP uses. Also, splitting does not take the allowed characters into account that the OP uses in the character class. It also does not take into account that there should be a single dot present in the fist part. – The fourth bird May 17 '20 at 11:17