0

I would expect this python script:

import re

path='assign-e398846de42d98b1f79109926d0da510.json'

print(path)

m = re.compile('[a-z0-9]{8,}')
print(m.match(path))

print(re.match('[\w\d]{8,}', path))

to behave similarly (i.e.: to match in the same places) as this perl one:

use strict;
$\ = "\n"; $, = "\t";
my $path='assign-e398846de42d98b1f79109926d0da510.json';
print($path);
print $path =~ /([\w\d]{8,})/;

However the output of the python scrip is:

assign-e398846de42d98b1f79109926d0da510.json
None
None

and perl returns:

assign-e398846de42d98b1f79109926d0da510.json
e398846de42d98b1f79109926d0da510

Why is that? How do I capture the 32 char md5 hash in the path name in python?

simone
  • 4,667
  • 4
  • 25
  • 47

2 Answers2

0

try this regex for md5-

re.findall(r"([a-fA-F\d]{32})", data)

#output
['e398846de42d98b1f79109926d0da510']

also, you need to use re.search instead of re.match

re.search('[\w\d]{8,}', path).group(0)

#output
'e398846de42d98b1f79109926d0da510'
Tal Folkman
  • 2,368
  • 1
  • 7
  • 21
0

Use m.search instead of m.match. The latter only matches the beginning of the string.

Amadan
  • 191,408
  • 23
  • 240
  • 301