提取文本和|之间的数字使用RegEx Python [英] Extract number between text and | with RegEx Python

查看:253
本文介绍了提取文本和|之间的数字使用RegEx Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取CVE|之间的信息,但仅提取CVE第一次出现在txt中.

I want to extract the information between CVE and |, but only the first time that CVE appear in the txt.

我现在有以下代码:

import re
f = open ('/Users/anna/PycharmProjects/extractData/DiarioOficial/aaa1381566.pdf.txt','r')
mensaje = f.read()
mensaje = mensaje.replace("\n","")

print re.findall(r'\sCVE\s+([^|]*)', mensaje)

这是txt文件:

CVE 1381566     

|     

Director: Juan Jorge Lazo Rodríguez    

Sitio Web:   

 www.diarioficial.cl    

|     

Mesa Central:   

 +562 2486 3600    

    Email:    

consultas@diarioficial.cl   

Dirección:    

Dr. Torres Boonen N°511, Providencia, Santiago, Chile.       

Este documento ha sido firmado electrónicamente de acuerdo con la ley N°19.799 e incluye sellado de tiempo y firma electrónica  

avanzada. Para verificar la autenticidad de una representación impresa del mismo, ingrese este código en el sitio web www.diarioficial.cl                           

DIARIO OFICIAL    

DE LA REPUBLICA DE CHILE    

Ministerio del Interior y Seguridad Pública      

V    

SECCIÓN       

CONSTITUCIONES, MODIFICACIONES Y DISOLUCIONES DE SOCIEDADES Y COOPERATIVAS                      

Núm. 42.031    

|    

Viernes 13 de Abril de 2018    

|    

Página 1 de 1      

Empresas y Cooperativas    

CVE 1381566        

EXTRACTO     

     

MARÍA SOLEDAD LÁSCAR MERINO, Notario Público Titular de la Sexta Notaría de  

Antofagasta, Prat Nº 482, local 25, certifica: Escritura hoy ante mí: CARLOS ANDRES ROJAS  

ANGEL, calle Antilhue Nº 1613; CAROLINA ANDREA ROJAS VALERO, calle Catorce de  

Febrero Nº 2339; NADIA TATIANA LEON BELMAR, calle Azapa Nº 4831; MARIO  

ANTONIO LUQUE HERRERA, calle Huanchaca Nº 398; PEDRO EDUARDO BARRAZA  

ZAPATA, Avenida Andrés Sabella Nº 2766; JOSE ANTONIO REYES RASSE, calle Altos del  

Mar Nº 1147, casa 15; y PATRICIA ALICIA MARCHANT ROJAS, calle Ossa N° 2741; todos  

domicilios Antofagasta, rectificaron y complementaron sociedad "CENTRO DE  

ACONDICIONAMIENTO FISICO LEFTRARU LIMITADA, LEFTRARU LIMITADA  

nombre de fantasía "LEFTRARU BOX LTDA"., constituida escritura este oficio, fecha 20 de  

febrero de 2018, publicada en extracto Diario Oficial fecha 13 de marzo de 2018, edición Nº  

42006; sentido señalar que la razón social correcta de la sociedad es: CENTRO DE  

ACONDICIONAMIENTO FISICO LEFTRARU LIMITADA; y su nombre de fantasía es  

LEFTRARU BOX LTDA.; y no "CENTRO DE ACONDICIONAMIENTO FISICO  

LEFTRARU, y nombre fantasía "LEFTRARU LTDA"., como erróneamente allí se menciona.-  

Demás estipulaciones escritura.- ANTOFAGASTA, 27 de marzo de 2018.-   

推荐答案

您可能要做的不是在开始时匹配\s,将空格字符匹配\s*零次或多次或断言字符串的开头^并使用搜索来找到第一个位置正则表达式模式会产生匹配项.

What you might do is instead of matching \s at the start, match a whitespace character\s*zero or more times or assert the start of the string ^ and use search to find the first location where the regular expression pattern produces a match.

然后从捕获组中获取值:

Then get the value from the capturing group:

mensaje = mensaje.replace("\n","")
regex = r"\s*CVE\s+([^|]*)"
matches = re.search(regex, mensaje)
if matches:
    print (matches.group(1).strip()) # 1381566

演示

这篇关于提取文本和|之间的数字使用RegEx Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆