2

I have few different types of strings and want to find a particular pattern or say first number coming after the pattern in the string.Following are the example strings.

str1 = mprage_scan_0006__002.nii
str2 = fgre3d_scan_0005__001+orig.HEAD
str3 = f3dgr2_scan_7_afni_009.nii
str4 = 2dfgre_scan_09_08.nii

I want to extract numbers after 'scan_?' in every string. if it's '007' or '09' or any other way, I want to extract only the number i.e '7', '9' etc..

i did try this but looks like my solution is not that flexible as its find the first number in the string instead of first number after 'scan_' pattern.

import re
a = re.findall(r'\d+', str[i])
scan_id = re.sub("^0+","",a[0])
1
  • int(str1.split('_scan_')[1].rsplit('_')[0], 10) Commented Apr 5, 2016 at 16:08

2 Answers 2

2

Try something like that using positive lookbehind assertion:

>>> import re
>>> 
>>> str1 = 'mprage_scan_0006__002.nii'
>>> str2 = 'fgre3d_scan_0005__001+orig.HEAD'
>>> str3 = 'f3dgr2_scan_7_afni_009.nii'
>>> str4 = '2dfgre_scan_09_08.nii'
>>> 
>>> pattern = r'(?<=scan_)0*(:?\d+)'
>>> 
>>> for s in [str1, str2, str3, str4]:
...     m = re.search(pattern, s)
...     print m.group(1)
... 
6
5
7
9
>>> 
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a reason to use lookbehind?
In this case probably it doesn't matter, but in general, it's more efficient not to capture something we don't want it the match, and lookbehind doesn't capture the assertion
It seems like it would be even more efficient to use a non-matching group then (by intuition, I don't have a reference for that).
2

Try with this one

a = re.findall(r'scan_0*(\d+)', str[i])

What it does:

  • look for scan_
  • followed by zero or more zeroes
  • followed by one ore more digits, and it selects this last group with the parentheses

3 Comments

the * symbol means 0 or more in regex. So it doesn't check if its followed by one or more zeros, it checks if its followed by 0 or more zeros
@Racialz Exactly what I said
@Racialz I'm sorry, I can't find the history of my answer. If you do you will see that I did not edit that part. Anyway thank you for pointing out the correct behaviour of symbol * in regex

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.