Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting a string without Space and without Number in the beginning

Here is my string:

^((\S)([a-z]))[a-zA-Z0-9_+-.]+@[a-zA-Z.-]+\.(edu|com|edu7|org)$\b

I need to check for 2 conditions in the beginning of a string:

  1. No space
  2. No number

My string satisfies the first condition but fails the second condition. Thank you for any suggestions. I did try regex101 but could not solve it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here are two email addresses that are both invalid:

  somebody@gmail.com  
5somebody@gmail.com  

I want neither of those returned by the program. My current code considers the second email as valid, which is incorrect.

>Solution :

Your expected matches imply that you want to only allow letters as the first char in the string, so you can use

^[a-zA-Z][a-zA-Z0-9_+.-]*@[a-zA-Z.-]+\.(?:edu7?|com|org)$

See the regex demo. Details:

  • ^ – start of string
  • [a-zA-Z] – an ASCII letter
  • [a-zA-Z0-9_+.-]* – zero or more letters, digits, _, +, . and - (note the position of the hyphen, it must be at the end of the character class)
  • @ – a @ char
  • [a-zA-Z.-]+ – one or more letters, dots or hyphens
  • \. – a dot
  • (?:edu7?|com|org)edu, edu7, com, org
  • $ – end of string.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading