简体   繁体   English

如何修复此正则表达式以提取某些特定字符?

[英]How to fix this regular expression in order to extract some specific characters?

I have a very_largeString that contains a list of words and and some id, i would like to extract all the words and it's id that have NC and AQ that morphologically ocurre consecutevely and print the rest of the id. 我有一个very_largeString,其中包含单词列表和一些ID,我想提取所有单词,并且它的ID具有NC和AQ形态上发生的字词,并打印其余的ID。 The output is just the word and it´s associated id for example: 输出仅是单词及其相关的ID,例如:

[('word','id'),('word','id')]

For example: 例如:

    very_largeString= ''' Hola hola I 1
compis compis NCMS000 0.500006
! ! Fat 1

No no RN 0.998045
sabía saber VMII3S0 0.592869
como como CS 0.999289
se se P00CN000 0.465639
ponía poner VMII3S0 0.65
una uno DI0FS0 0.951575
lavadora lavadora NCFS000 0.414738
hasta hasta SPS00 0.957698
que que PR0CN000 0.562517
conocí conocer VMIS1S0 1
esta este DD0FS0 0.986779
y y CC 0.999962
es ser VSIP3S0 1
que que CS 0.437483
es ser VSIP3S0 1
muy muy RG 1
sencilla sencillo AQ0FS0 1
de de SPS00 0.999984
utilizar utilizar VMN0000 1
! ! Fat 1

Todo todo DI0MS0 0.560961
un uno DI0MS0 0.987295
gustazo gustazo NCMS000 1
cuando cuando CS 0.985595
estamos estar VAIP1P0 1
aprendiendo aprender VMG0000 1
para para SPS00 0.999103
emancipar emancipar VMN0000 1
nos nos PP1CP000 1
, , Fc 1
que que CS 0.437483
si si CS 0.99954
nos nos PP1CP000 0.935743
ponen poner VMIP3P0 1
facilidad facilidad NCFS000 1
con con SPS00 1
las el DA0FP0 0.970954
tareas tarea NCFP000 1
de de SPS00 0.999984
la el DA0FS0 0.972269
casa casa NCFS000 0.979058
pues pues CS 0.998047
mejor mejor AQ0CS0 0.873665
que que PR0CN000 0.562517
mejor mejor AQ0CS0 0.873665
. . Fp 1

Antes_de antes_de SPS00 1
esta este PD0FS000 0.0132212
teníamos tener VMII1P0 1
otra otro DI0FS0 0.803899
de de SPS00 0.999984
la el DA0FS0 0.972269
marca marca NCFS000 0.972603
Otsein otsein NP00000 1
, , Fc 1
de de SPS00 0.999984
estas este DD0FP0 0.97043
que que PR0CN000 0.562517
van ir VMIP3P0 1
incluidas incluir VMP00PF 1
en en SPS00 1
el el DA0MS0 1
mobiliario mobiliario NCMS000 0.476077
y y CC 0.999962
además además RG 1
era ser VSII1S0 0.491262
de de SPS00 0.999984
carga carga NCFS000 0.952569
superior superior AQ0CS0 0.992424
, , Fc 1
pero pero CC 0.999764
tan tan RG 1
antigua antiguo AQ0FS0 0.953488
que que CS 0.437483
según según SPS00 0.995943
mi mi DP1CSS 0.999101
madre madre NCFS000 1
, , Fc 1
nadie nadie PI0CS000 1
la lo PP3FSA00 0.0277039
podía poder VMII3S0 0.63125
tocar tocar VMN0000 1
porque porque CS 1
solo solo RG 0.0472103
la lo PP3FSA00 0.0277039
entendía entender VMII3S0 0.65
ella él PP3FS000 1
. . Fp 1

Esta este PD0FS000 0.0132212
es ser VSIP3S0 1
de de SPS00 0.999984
la el DA0FS0 0.972269
marca marca NCFS000 0.972603
Aeg aeg NP00000 1
y y CC 0.999962
dentro_de dentro_de SPS00 1
este este DD0MS0 0.960092
tipo tipo NCMS000 1
de de SPS00 0.999984
lavadoras lavadora NCFP000 0.411969
de de SPS00 0.999984
esta este DD0FS0 0.986779
marca marca NCFS000 0.972603
las lo PP3FPA00 0.0289466
había haber VAII1S0 0.353863
más más RG 1
caras caro AQ0FP0 0.417273
o o CC 0.999769
más más RG 1
baratas barato AQ0FP0 0.3573
y y CC 0.999962
está estar VAIP3S0 0.999201
digamos decir VMSP1P0 0.785925
que que CS 0.437483
está estar VAIP3S0 0.999201
en en SPS00 1
el el DA0MS0 1
punto punto NCMS000 1
medio medio AQ0MS0 0.314286
. . Fp 1

Es ser VSIP3S0 1
de de SPS00 0.999984
color color NCMS000 1
blanco blanco AQ0MS0 0.598684
y y CC 0.999962
tiene tener VMIP3S0 1
carga carga NCFS000 0.952569
frontal frontal AQ0CS0 0.657209
, , Fc 1
con con SPS00 1
una uno DI0FS0 0.951575
capacidad capacidad NCFS000 1
de de SPS00 0.999984
6kg 6kg Z 1
. . Fp 1

En en SPS00 1
casa casa NCFS000 0.979058
a_pesar_de a_pesar_de SPS00 1
ser ser VSN0000 0.940705
cuatro 4 Z 1
, , Fc 1
se se P00CN000 0.465639
ponen poner VMIP3P0 1
lavadoras lavadora NCFP000 0.411969
casi casi RG 1
todos todo DI0MP0 0.624221
o o CC 0.999769
todos todo DI0MP0 0.624221
los el DA0MP0 0.976481
días día NCMP000 1
. . Fp 1

En en SPS00 1
su su DP3CS0 1
parte parte NCFS000 0.499183
de de SPS00 0.999984
arriba arriba RG 0.986014
encontramos encontrar VMIP1P0 0.65
la el DA0FS0 0.972269
" " Fe 1
; ; Fx 1
zona zona NCFS000 1
de de SPS00 0.999984
mandos mando NCMP000 1
" " Fe 1
; ; Fx 1
, , Fc 1
donde donde PR000000 0.967437
se se P00CN000 0.465639
puede poder VMIP3S0 0.999117
echar echar VMN0000 1
el el DA0MS0 1
detergente detergente NCMS000 0.49273
, , Fc 1
aunque aunque CC 1
en en SPS00 1
nuestro nuestro DP1MSP 0.94402
caso caso NCMS000 0.99812
lo lo PP3CNA00 0.271177
a a SPS00 1
el el DA0MS0 1
ser ser VSN0000 0.940705
gel gel NCMS000 1
lo lo PP3CNA00 0.271177
ponemos poner VMIP1P0 1
directamente directamente RG 1
junto_con junto_con SPS00 1
la el DA0FS0 0.972269
ropa ropa NCFS000 1
. . Fp 1

Luego luego RG 0.996689
tiene tener VMIP3S0 1
la el DA0FS0 0.972269
rueda rueda NCFS000 0.72093
para para SPS00 0.999103
elegir elegir VMN0000 1
el el DA0MS0 1
programa programa NCMS000 0.953488
y y CC 0.999962
los el DA0MP0 0.976481
intermitentes intermitente NCMP000 0.342773
que que PR0CN000 0.562517
indican indicar VMIP3P0 1
en en SPS00 1
que que CS 0.437483
paso paso NCMS000 0.905738
de de SPS00 1
el el DA0MS0 1
programa programa NCMS000 0.953488
estaba estar VAII1S0 0.5
. . Fp 1

Como como CS 0.999289
todas todo PI0FP000 0.0490506
tiene tener VMIP3S0 1
programas programa NCMP000 0.97619
más más RG 1
cortos corto AQ0MP0 1
y y CC 0.999962
más más RG 1
largos largo AQ0MP0 0.97619
, , Fc 1
incluso incluso RG 0.996383
un uno DI0MS0 0.987295
programa programa NCMS000 0.953488
que que PR0CN000 0.562517
seria seriar VMIP3S0 0.151546
como como CS 0.999289
lavar lavar VMN0000 1
a a SPS00 0.996023
mano mano NCFS000 0.992095
y y CC 0.999962
otro otro DI0MS0 0.612994
ideal ideal NCMS000 0.5
para para SPS00 0.999103
estores estor NCMP000 1
, , Fc 1
que que PR0CN000 0.562517
salen salir VMIP3P0 0.972603
casi casi RG 1
secos seco AQ0MP0 1
y y CC 0.999962
planchaditos planchar VMP00PM 0.691767
para para SPS00 0.999103
colgar colgar VMN0000 1
y y CC 0.999962
ya ya RG 0.999395
está estar VAIP3S0 0.999201
. . Fp 1

Es ser VSIP3S0 1
muy muy RG 1
fácil fácil AQ0CS0 1
de de SPS00 0.999984
aprender aprender VMN0000 1
la lo PP3FSA00 1
y y CC 0.999962
además además RG 1
tiene tener VMIP3S0 1
indicador indicador NCMS000 0.64273
por por SPS00 1
sonido sonido NCMS000 1
de de SPS00 0.999984
cuando cuando CS 0.985595
acaba acabar VMIP3S0 0.992958
, , Fc 1
lista listar VMIP3S0 0.220088
para para SPS00 0.999103
abrir abrir VMN0000 1
y y CC 0.999962
tender tender VMN0000 1
. . Fp 1

Por por SPS00 1
decir decir VMN0000 0.997512
algo algo PI0CS000 0.900246
malo malo AQ0MS0 0.657087
de de SPS00 0.999984
ella él PP3FS000 1
, , Fc 1
sería ser VSIC1S0 0.5
que que CS 0.437483
cuando cuando CS 0.985595
centrifuga centrifugar VMIP3S0 0.994859
, , Fc 1
algo algo PI0CS000 0.900246
que que PR0CN000 0.562517
hace hacer VMIP3S0 1
muy muy RG 1
bien bien RG 0.902728
, , Fc 1
pues pues CS 0.998047
vibra vibrar VMIP3S0 0.994856
un uno DI0MS0 0.987295
poco poco RG 0.542435
y y CC 0.999962
se se P00CN000 0.465639
nota notar VMIP3S0 0.419995
el el DA0MS0 1
ruido ruido NCMS000 1
jeje jeje NCMS000 0.833445
, , Fc 1
pero pero CC 0.999764
no no RN 0.998134
se se P00CN000 0.465639
mueve mover VMIP3S0 0.994868
de de SPS00 0.999984
su su DP3CS0 1
sitio sitio NCMS000 0.980769
! ! Fat 1
! ! Fat 1

Saludillos saludillos NP00000 0.411768
! ! Fat 1

'''

this will be the desired output, since they have at the begining of the id the NC and AQ characters: 这将是所需的输出,因为它们在id的开头具有NC和AQ字符:

[('carga', 'NCFS000', 'superior', 'AQ0CS0'), ('carga', 'NCFS000', 'frontal', 'AQ0CS0')]

How can i fix my regex in order to extract all the words that have as id AQ and NC?. 我如何修复我的正则表达式,以便提取所有具有id AQ和NC的单词? This is what i all ready tried: 这是我所有人准备尝试的内容:

from pprint import pprint
import re
result = re.findall(r'''
    (?mx)              # Muti-line, verbose
    ^                  # Align to beginning of a line
    (\S+)\s+           # Grab first word
    \S+\s+             # Don't care about 2nd word
    ((?:AQ|NC)\S+)\s+  # 3rd word must have AQ or NC
    \S+\n              # End of first line
    ^                  # Next line is identical in form
    (\S+)\s+           # to the first line
    \S+\s+       
    ((?:AQ|NC)\S+)\s+ 
    \S+\n
''', very_largeString)
pprint (result)

This is the output: 这是输出:

[('carga', 'NCFS000', 'superior', 'AQ0CS0'),
 ('punto', 'NCMS000', 'medio', 'AQ0MS0'),
 ('color', 'NCMS000', 'blanco', 'AQ0MS0'),
 ('carga', 'NCFS000', 'frontal', 'AQ0CS0'),
 ('ruido', 'NCMS000', 'jeje', 'NCMS000')]

The problem with the output is that is not preserving the desired ocurrence as you can see at the last of the list: ('ruido', 'NCMS000', 'jeje', 'NCMS000') . 输出的问题是,不能保留所需的出现,如清单最后所示:( ('ruido', 'NCMS000', 'jeje', 'NCMS000') How can i solve the regex in order to get only the words that have NC and AQ id? 我如何解决正则表达式,以便仅获取具有NC和AQ ID的单词?

explain why this doesnt work? 解释为什么这不起作用?

print re.findall("(\S+)\s+((?:NC|AQ)\S+)",text)

or if you need them paired with NC on one line and AQ on next then 或者如果您需要将它们与NC配对并在一行上与AQ配对,则

print re.findall("(\S+)\s+(NC\S+).*\n.*\s(\S+)\s+(AQ\S+)",text)

or if you need them paired in any order and 2 NC entries can work 或者,如果您需要按任意顺序配对它们,则可以使用2个NC条目

print re.findall("(\S+)\s+((?:NC|AQ)\S+).*\n.*\s(\S+)\s+((?:NC|AQ)\S+)",text)

一种方法是,您可以通过以下方式过滤掉重复项:

print [r for r in result if r[1][0:2]!=r[3][0:2]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM