0

I have a series of file names of the form "J9.0_Feature_ML_111.json". I need to retrieve the integer number after the J and before the "."

I can get it on Pythex.org using (?<=J)(\d+) but when I try and implement it in Python using

import re
filename = 'J9.0_Feature_ML_111.json'
num = re.match(r"(?<=J)(\d+)", filename).group()

I get a NoneType object returned. Is anyone able to help me with this please? I'm using Python 3.5.4. conda list doesn't give me a version of re so I assume it's part of the base package?

jlt199
  • 2,349
  • 6
  • 23
  • 43
  • I disagree this is an exact dulpicate of the question highlighted by Wiktor Stribizew, but it has given me the answer. I should using `re.search` and not `re.match`. I don't understand why, but it seems to work. – jlt199 Mar 05 '18 at 15:23
  • 3
    "but it has given me the answer" That is the whole point of "duplicates". Two questions are rarely ever *exact* duplicates. However, when a question is marked as a "duplicate" we often the answers to the two questions are the same, or at least similar. – Code-Apprentice Mar 05 '18 at 15:29
  • 1
    Per the [`re` documentation](https://docs.python.org/3/library/re.html?highlight=matching%20searching#regular-expression-syntax), emphasis mine: "Note that patterns which start with positive lookbehind assertions **will not match at the beginning of the string being searched**" Since you're using a positive lookbehind & `re.match()` searches at the beginning of the string, it's going to return `None`. The documentation further states: "you will most likely want to use the `search()` function rather than the `match()` function" – sco1 Mar 05 '18 at 15:46

0 Answers0