Question about from_name_re labeling based on regex expression

Hi everyone,

I have a question about how the ImageDataBunch.from_name_re create labels.

For example, in the lesson 1, below is a path name:
PosixPath(’/root/.fastai/data/oxford-iiit-pet/images/Bengal_159.jpg’)
and, below is the pattern:
pat = r’/([^/]+)_\d+.jpg$’

I understand that that regex is used to find names, like “Bengal_159.jpg” above.

What I don’t understand is, how did from_name_re know to label that photo with just “Bengal” without “_159”?

I went through the source code of from_name_re but still confused.

Thank you for your help!

its capturing groups within the sentence - try this exercise from regexone

string = '/home/Bengal_159.jpg'
pattern = r'/([^/]+)_\d+.jpg$'
match = re.search(pattern, string) 
match.group(1)
>>> 'Bengal'

"""
[^/]       not a slash
[^/]+      anything that is not a slash
([^/]+)    call this "anything" group 1
"""
2 Likes

I understand now. Thank you.