Difference between revisions of "Python: Regex"

From RHS Wiki
Jump to navigation Jump to search
m
Tag: visualeditor
Tag: visualeditor
Line 10: Line 10:
 
                         r'[1-9][0-9]|'
 
                         r'[1-9][0-9]|'
 
                         r'[1-9])'
 
                         r'[1-9])'
                         r'\b')</source>
+
                         r'\b')
 +
port_regex = r'([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])'</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>
Line 43: Line 44:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Phone Number ===
+
===Phone Number===
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
sep = '(:?\s+|-|\.)?' # separator
 
sep = '(:?\s+|-|\.)?' # separator
Line 55: Line 56:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==== Spain Phone ====
+
====Spain Phone====
 
<syntaxhighlight lang="python">
 
<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})"
 
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>
 
</syntaxhighlight>

Revision as of 09:38, 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')
port_regex = r'([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])'

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"

Phone Number

sep = '(:?\s+|-|\.)?' # separator
phone_re = re.compile(r'''
  (\d{3}|\(\d{3}\))  # area code
  {sep}              # separator
  (\d{3})            # first 3
  {sep}              # separator
  (\d{4})            # last 4
'''.format(sep=sep), re.VERBOSE)

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})"