In Python django, if I want the regex to detect from 6 characters to infinity, [\ w -] {6, infinite} what is the quantifier?

4
url(r'^(?P<foo>[\w-]{6, })/$', view)

In django if I want the regex to detect from 6 characters to infinity, {6, infinito} , do I leave it blank, or how should I express it?

This way it works but I'm not sure ...

    
asked by Andrés Muñoz 07.02.2017 в 04:30
source

1 answer

4

6 or more alphanumerics, _ and / or -

^[-\w]{6,}$
  • The {6,} quantifier goes without spaces, with nothing, in the 2nd parameter.
  • It is unnecessary to use the group (?P<foo> ... ) in this case, although it does not affect the result.
  • Delimiters like / are not used in Python.
  • It is good practice to include the - script as the first character within a character class.


Code

import re
# re es el módulo necesario para usar expresiones regulares
# https://docs.python.org/3/library/re.html

regex = r'^[-\w]{6,}$'
texto = 'abc123'

if re.search(regex, texto):
    print('Coincide')
else:
    print('No coincide')

Demo: link


You can call:

  • re.search () that searches for a match anywhere in the string.

    re.search(r'^[-\w]{6,}$', str)
    
  • re.fullmatch () that searches only matches all the string.

    re.fullmatch(r'[-\w]{6,}', str)
    


Other quantifiers

  Cuant.   Descripción                             
 -------- ------------------------------------------------------------------ 
  ?        1 o 0                                                             
  ??       0 o 1                                                             
  *        infinito a 0                                                      
  *?       0 a infinito                                                      
  +        infinito a 1                                                      
  +?       1 a infinito                                                      
  {n}      exactamente n repeticiones                                      
  {m,n}    hasta n repeticiones, mínimo m                         
  {m,n}?   mínimo m, hasta n repeticiones
  {m,}     entre m e infinitas repeticiones, cuantos más puedan coincidir    
  {m,}?    entre m e infinitas repeticiones, cuantas menos puedan coincidir
  {,n}     entre 0 y n repeticiones (sólo Python y Ruby)


Examples:

  Cuant.    Descripción                             
 --------- --------------------------------------------------------------------
  \w+       1 o más alfanuméricos (incluye "_"), cuantas más puedan coincidir
  \d{6}     exactamente 6 dígitos consecutivos
  x{3,6}    3 a 6 letras "x" consecutivas, cuantas más puedan coincidir
  @{2,5}?   2 a 5 arrobas consecutivas, cuantas menos puedan coincidir
    
answered by 07.02.2017 в 06:59