Difference between revisions of "Python: Regex"

From RHS Wiki
Jump to navigation Jump to search
Tag: visualeditor
Line 1: Line 1:
=== Port ===
+
===Port===
<source lang="python">port_regex = re.compile(r'\b('
+
<source lang="python">port_regex = re.compile(r'\b('
 
                         r'6553[0-5]|'
 
                         r'6553[0-5]|'
 
                         r'655[0-2][0-9]|'
 
                         r'655[0-2][0-9]|'
Line 11: Line 11:
 
                         r'[1-9])'
 
                         r'[1-9])'
 
                         r'\b')</source>
 
                         r'\b')</source>
=== IP ===
+
===IP===
<source lang="python">ip = re.compile('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')</source>
+
<source lang="python">ip = re.compile('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')</source>
  
=== NIE ===
+
===NIE===
 
  r'^[XYZ]\d{7}[ABCDEFGHJKLMNPQRSTVWXYZ]'
 
  r'^[XYZ]\d{7}[ABCDEFGHJKLMNPQRSTVWXYZ]'
  
=== DNI ===
+
===DNI===
 
  r'\d{8}[ABCDEFGHJKLMNPQRSTVWXYZ]'
 
  r'\d{8}[ABCDEFGHJKLMNPQRSTVWXYZ]'
  
=== Spanish License Plates ===
+
===Spanish License Plates===
 
  r'\w{0,2}\d{4}\w{1,3}'
 
  r'\w{0,2}\d{4}\w{1,3}'
 +
 +
=== E-MAIL ===
 +
<syntaxhighlight lang="python3">
 +
EMAIL = r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
 +
</syntaxhighlight>
 +
 +
=== IBAN ===
 +
<syntaxhighlight lang="python">
 +
IBAN = r"[a-zA-Z]{2}[0-9]{2} ?[a-zA-z0-9]{0,4} ?[a-zA-z0-9]{0,4} ?[a-zA-z0-9]{0,3}(?:[a-zA-z0-9] ?[a-zA-z0-9]{0,4} ?[a-zA-z0-9]{0,4} ?[a-zA-z0-9]{0,4} ?[a-zA-z0-9]{0,3})?"
 +
</syntaxhighlight>
 +
 +
=== Payment Account Number ===
 +
<syntaxhighlight lang="python">
 +
PAN = r"\b(?:\d[ -]*?){13,19}\b"
 +
</syntaxhighlight>
 +
 +
=== Zip Code ===
 +
<syntaxhighlight lang="python">
 +
ZIP_CODE_SPAIN = r"0[1-9][0-9]{3}|\D[1-4][0-9]{4}|\D5[0-2][0-9]{3}\D"
 +
</syntaxhighlight>Spain Phone<syntaxhighlight lang="python">
 +
spainphones = r"(?:(?:\+?34(?:[ \t|\-])?)?[9|6|7](?:(?:\d{1}(?:[ \t|\-])?[0-9]{3})|(?:\d{2}(?:[ \t|\-])?[0-9]{2}))(?:[ \t|\-])?[0-9]{2}(?:[ \t|\-])?[0-9]{2})"
 +
</syntaxhighlight>

Revision as of 08:32, 24 April 2019

Port

port_regex = re.compile(r'\b('
                        r'6553[0-5]|'
                        r'655[0-2][0-9]|'
                        r'65[0-4][0-9][0-9]|'
                        r'6[0-4][0-9][0-9][0-9]|'
                        r'[1-5][0-9][0-9][0-9][0-9]|'
                        r'[1-9][0-9][0-9][0-9]|'
                        r'[1-9][0-9][0-9]|'
                        r'[1-9][0-9]|'
                        r'[1-9])'
                        r'\b')

IP

ip = re.compile('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')

NIE

r'^[XYZ]\d{7}[ABCDEFGHJKLMNPQRSTVWXYZ]'

DNI

r'\d{8}[ABCDEFGHJKLMNPQRSTVWXYZ]'

Spanish License Plates

r'\w{0,2}\d{4}\w{1,3}'

E-MAIL

EMAIL = r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"

IBAN

IBAN = r"[a-zA-Z]{2}[0-9]{2} ?[a-zA-z0-9]{0,4} ?[a-zA-z0-9]{0,4} ?[a-zA-z0-9]{0,3}(?:[a-zA-z0-9] ?[a-zA-z0-9]{0,4} ?[a-zA-z0-9]{0,4} ?[a-zA-z0-9]{0,4} ?[a-zA-z0-9]{0,3})?"

Payment Account Number

PAN = r"\b(?:\d[ -]*?){13,19}\b"

Zip Code

ZIP_CODE_SPAIN = r"0[1-9][0-9]{3}|\D[1-4][0-9]{4}|\D5[0-2][0-9]{3}\D"

Spain Phone

spainphones = r"(?:(?:\+?34(?:[ \t|\-])?)?[9|6|7](?:(?:\d{1}(?:[ \t|\-])?[0-9]{3})|(?:\d{2}(?:[ \t|\-])?[0-9]{2}))(?:[ \t|\-])?[0-9]{2}(?:[ \t|\-])?[0-9]{2})"