0

I have tried finding a python regular expression to match the following lines, and and my interest being to extract the portion of each line between "|" and "." (preceding upx). My attempt was:

pattern=compile.re(re"^\S+\|(\S+).upx\.+")

But it did not work

My data:

UMM_189|XXYT9888_UMX_5711769.upx_OWED_786_bopsio_34527_sen_72.345615
AMW_126|7010.upx_XAWA01266525261
QEA_234|Serami_bolismun_milte_1_UMM1.upx_YU_168145
MMP_377|723C_UMM_5711781.upx_UXA_2_serax_78120_ser_23.26255277

My expected output:

XXYT9888_UMX_5711769
7010
Serami_bolismun_milte_1_UMM1
723C_UMM_5711781

Any better ideas please?

1
  • @iCodez,Thanks for spotting the error. Now corrected. Commented Nov 4, 2014 at 18:46

4 Answers 4

2

I do not think that Regex is necessary here because your data is pretty ordered. A list comprehension with str.split and str.splitlines will suffice:

>>> data = '''\
... UMM_189|XXYT9888_UMX_5711769.upx_OWED_786_bopsio_34527_sen_72.345615
... AMW_126|7010.upx_XAWA01266525261
... QEA_234|Serami_bolismun_milte_1_UMM1.upx_YU_168145
... MMP_377|723C_UMM_5711781.upx_UXA_2_serax_78120_ser_23.26255277
... '''
>>> [x.split('|', 1)[1].split('.upx', 1)[0] for x in data.splitlines()]
['XXYT9888_UMX_5711769', '7010', 'Serami_bolismun_milte_1_UMM1', '723C_UMM_5711781']
>>>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

>>> re.findall(r'\|(.*?)\.',data)
['XXYT9888_UMX_5711769', '7010', 'Serami_bolismun_milte_1_UMM1', '723C_UMM_5711781']

Comments

0
import re
your_str = "UMM_189|XXYT9888_UMX_5711769.upx_OWED_786_bopsio_34527_sen_72.345615"
result = re.match(r'^[A-Z]{3}_[0-9]{3}\|(?P<id>[A-Za-z0-9_]*).upx*', your_str)
print result.group('id')

1 Comment

Please add some explanation to your answer. Code-only answers are sometimes good enough, but code+explanation answers are always better
0

You have the slash dot and dot backwards. Try pattern=compile.re(re"^\S+\|(\S+)\.upx.+")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.