Saltar al contenido

usar expresiones regulares en el ejemplo de código de Python

Ejemplo 1: expresión regular de Python

import re

# The string you want to find a pattern within
test_string = 'Hello greppers!'

# Creating a regular expression pattern
# This is a simple one which finds "Hello"
pattern = re.compile(r'Hello')

# This locates and returns all the occurences of the pattern
# within the test_string
match = pattern.finditer(test_string)

# Outputs all the ocurrences which were returned as 
# as match objects
for match in matches:
  print(match)

Ejemplo 2: documentación de Python Regex

>>> import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'

Ejemplo 3: sintaxis de expresión regular python

1. A fixed string -> abc123
2. Arbitrary repetition -> a*b ( "*" means that you can have an arbitrary
								number (possibly 0) of the previous char
3. Repeat character at least once -> a+b # ab, aaaab
4. Repeat character at most once -> a?b # b, ab
5. Repeat a character a fixed number of timers -> a{5} # aaaaa
6. Repeat a pattern a fixed number of times -> (a*b){3} # baabab, ababaaaab
7. Repeat a character or pattern a variable number of times -> a{2,4} # aa, aaa, aaaa
8. Choice of several characters -> [ab]c # ac, bc
9. Arbitrary mixture of several characters -> [ab]*c # c, aac, abbac
10. Ranges of characters -> [A-H][a-z]* # Aasdfalsd, Hb, G 
11. Characters OTHER than particular one -> [^AB] # C, D
12. Choice of several expressions -> Dr|Mr|Ms|Mrs # Dr, Mr, Mrs, Ms
13. Nesting expressions -> ([A-Z][a-z][0-9])* # A, AzSDFcvfg
14. Start of a line -> ^ab
15. End of a line -> ab$

#Type of pattern
1. Special characters -> [ # [
2. Any charactter 'except' newline -> . # a, *, -
3. Nongreedy evaluation -> <.*>? # <h1></h2 name = "foo">
4. Whitespace -> s

Ejemplo 4: expresiones regulares en python

# pip install regex
import re
# simple find all
sample = "Nothing lasts... but nothing is lost"
found = re.findall("thing", sample)
print(found)

Ejemplo 5: expresiones regulares en Python

>>> import re
>>> p = re.compile('ab*')
>>> p
re.compile('ab*')
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *