Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

609
Views
Capturar grupos con Expresión Regular (Python)

Una especie de novato aquí, disculpas si me equivoco.

Estoy aprendiendo expresiones regulares y estoy en esta lección: https://regexone.com/lesson/capturing_groups .

En el intérprete de python, trato de usar los paréntesis para capturar solo lo que precede a la parte .pdf de la cadena de búsqueda, pero mi resultado lo captura a pesar de usar los paréntesis. ¿Qué estoy haciendo mal?

 import re string_one = 'file_record_transcript.pdf' string_two = 'file_07241999.pdf' string_three = 'testfile_fake.pdf.tmp' pattern = '^(file.+)\.pdf$' a = re.search(pattern, string_one) b = re.search(pattern, string_two) c = re.search(pattern, string_three) print(a.group() if a is not None else 'Not found') print(b.group() if b is not None else 'Not found') print(c.group() if c is not None else 'Not found')

Devoluciones

 file_record_transcript.pdf file_07241999.pdf Not found

pero debe volver

 file_record_transcript file_07241999 Not found

¡Gracias!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Necesitas el primer grupo capturado:

 a.group(1) b.group(1) ...

sin ninguna especificación de grupo capturada como argumento para group() , mostrará la coincidencia completa, como lo que está obteniendo ahora.

Aquí hay un ejemplo:

 In [8]: string_one = 'file_record_transcript.pdf' In [9]: re.search(r'^(file.*)\.pdf$', string_one).group() Out[9]: 'file_record_transcript.pdf' In [10]: re.search(r'^(file.*)\.pdf$', string_one).group(1) Out[10]: 'file_record_transcript'
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!