Search code examples
phpregexlaravelpreg-match

Calculating if an email fits within a regex?


So I am trying to determine if someone is using a temporary email made by our system. If a user tries to login with a social account (Twitter / Facebook) and they decline access to email I generate an email for our system which is AccountID@facebook.com or AccountID@twitter.com so an example would be 123456789@facebook.com. This is a temporary email until a user enters a real email. I am trying to compare this using regex.

    if (preg_match("/^[0-9]@twitter.com/", Auth::user()->email, $matches)) {
    }

However I think my regex is incorrect. How would one check if the format of a string is N Number of digits followed by @twitter.com or @facebook.com


Solution

  • How would one check if the format of a string is N Number of digits followed by @twitter.com or @facebook.com

    You can use this regex:

    '/^\d+@(?:facebook|twitter)\.com$/'
    

    You are using ^[0-9]@ which will allow for only single digit at start. Besides DOT is a special character in regex that needs to be escaped. Also note use of end anchor $ in your anchor to avoid matching unwanted input.