RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find SPF checker
Expression
^v=spf1[ \t]+[+?~-]?(?:(?:all)|(?:ip4(?:[:][0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})?(?:/[0-9]{1,2})?)|(?:ip6(?:[:]([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})?(?:/[0-9]{1,2})?)|(?:a(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+)?(?:/[0-9]{1,2})?)|(?:mx(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+)?(?:/[0-9]{1,2})?)|(?:ptr(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+))|(?:exists(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+))|(?:include(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+))|(?:redirect(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+))|(?:exp(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+))|)(?:(?:[ \t]+[+?~-]?(?:(?:all)|(?:ip4(?:[:][0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})?(?:/[0-9]{1,2})?)|(?:ip6(?:[:]([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})?(?:/[0-9]{1,2})?)|(?:a(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+)?(?:/[0-9]{1,2})?)|(?:mx(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+)?(?:/[0-9]{1,2})?)|(?:ptr(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+))|(?:exists(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+))|(?:include(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+))|(?:redirect(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+))|(?:exp(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+))|))*)?$
Description
I use "preg_match" function on PHP with this expression. Detects a valid SPF expression. Warnings: ip6 register only works in "no compressed" form. "trim" the string before use the expression (or add [ \t]+ after ^ and before $)
Matches
v=spf1 +a +mx -all | v=spf1 +a +mx +ip4:192.168.0.0/24 +ptr:mydomain.com -all | v=spf1 include:spf.alternative.com
Non-Matches
v=spf1 | v=spf2 -all | v=spf1 *all
Author Rating: The rating for this expression. Oscar Martín
Source
Your Rating
Bad Good

Enter New Comment

Title

Name

Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: version 1.1
Name: Fabian
Date: 2/9/2017 4:46:37 PM
Comment:
Hi, I did have some issues with _ in domain names, ip6 and redirect= I did make some minor adjustments: ^v=spf1[ \t]+[+?~-]?(?:(?:all)|(?:ip4(?:[:][0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})?(?:/[0-9]{1,2})?)|(?:ip6(?:[:]([0-9A-Fa-f]{1,4}:){1,7}[:0-9A-Fa-f]{1,4})?(?:/[0-9]{1,2})?)|(?:a(?:[:][\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+)?(?:/[0-9]{1,2})?)|(?:mx(?:[:][\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+)?(?:/[0-9]{1,2})?)|(?:ptr(?:[:][\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+))|(?:exists(?:[:][_A-Za-z0-9](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+))|(?:include(?:[:][_A-Za-z0-9](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+))|(?:redirect([=:]{1}[\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+))|(?:exp(?:[:][\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+))|)(?:(?:[ \t]+[+?~-]?(?:(?:all)|(?:ip4(?:[:][0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})?(?:/[0-9]{1,2})?)|(?:ip6(?:[:]([0-9A-Fa-f]{1,4}:){1,7}[:0-9A-Fa-f]{1,4})?(?:/[0-9]{1,2})?)|(?:a(?:[:][\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+)?(?:/[0-9]{1,2})?)|(?:mx(?:[:][\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+)?(?:/[0-9]{1,2})?)|(?:ptr(?:[:][\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+))|(?:exists(?:[:][\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+))|(?:include(?:[:][\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+))|(?:redirect([=:]{1}[\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+))|(?:exp(?:[:][\w-](?:[\w-]*[\w-])?(?:\.[\w-](?:[\w-]*[\w-])?)+))|))*)?$


Title: Slight modification
Name: Marcus
Date: 5/14/2014 10:00:08 AM
Comment:
Does not work for ptr without explicitly specified domain. This modification extends the regex respectively ^v=spf1[ \t]+[+?~-]?(?:(?:all)|(?:ip4(?:[:][0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]\ {1,3})?(?:/[0-9]{1,2})?)|(?:ip6(?:[:]([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})?(?:/[0-9]{1,2})?)|(?:a(?:[:][A-Z\ a-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+)?(?:/[0-9]{1,2})?)|(?:mx(\ ?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+)?(?:/[0-9]{1,2})\ ?)|(?:ptr(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+)?)|(?:\ exists(?:[:][A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-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