Home » Php » php – Regex not contain at end and start differents string

php – Regex not contain at end and start differents string

Posted by: admin February 25, 2020 Leave a comment

Questions:

i want to take only mail address which not contains ‘xxx-‘ at the start and ‘@test.com’ at the end of the mail string.

Here my code :

->where('email', 'regexp', "/^(([email protected]).)*$/i")

Here i take only email where not contains ‘test.com’ at the end.
I want to not take email where the start is ‘xxx-‘ too. Anyone know how to do this ?
Thank you !

How to&Answers:

You may use this regex with 2 negative lookahead assertions (rules):

^(?!xxx-)(?![^@][email protected]\.com$)[^@][email protected][^@]+$

RegEx Demo

RegEx Details:

  • ^: Start
  • (?!xxx-): Don’t allow email to start with xxx-
  • (?![^@][email protected]\.com$): Don’t allow email to end with test.com
  • [^@][email protected][^@]+: Match an email that must have only one @
  • $: End