Home > Net >  Selecting a string without Space and without Number in the beginning
Selecting a string without Space and without Number in the beginning

Time:02-06

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.

Here are two email addresses that are both invalid:

  [email protected]  
[email protected]  

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

CodePudding user response:

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.
  •  Tags:  
  • Related