0

The regex works in js.

let re = /ses-[a-zA-Z0-9]*[_-][a-zA-Z0-9]*?_(.*?)/g;
let result = re.exec('/ieeg_epilepsy/derivatives/brainvisa/sub-01/ses-pre/default_analysis/segmentation/mesh/sub-01_ses-pre_T1w_Lwhite.gii.minf');
console.log(result)
// result is ["ses-pre_T1w_", "", index: 94, input: "/ieeg_epilepsy/derivatives/brainvisa/sub-01/ses-pr…mentation/mesh/sub-01_ses-pre_T1w_Lwhite.gii.minf", groups: undefined]

But when I try to translate js code to python code, the result is always a empty list.

import res

relative_path = '/ieeg_epilepsy/derivatives/brainvisa/sub-01/ses-pre/default_analysis/segmentation/mesh/sub-01_ses-pre_T1w_Lwhite.gii.minf'
result = re.findall(
    r"/ses-[a-zA-Z0-9]*[_-][a-zA-Z0-9]*?_(.*?)/g", relative_path)

print(result) # []

any ideas ?

Harrymissu
  • 450
  • 2
  • 8
  • 18
  • 1
    `re.findall(r"ses-[a-zA-Z0-9]*[_-][a-zA-Z0-9]*_(.*)", relative_path)`, note `.*?` at the end of the regex always matches an empty string. – Wiktor Stribiżew Aug 11 '21 at 21:15
  • 1
    The capture group `(.*?)` does not match anything, and is returned by re.findall. It could be `ses-[a-zA-Z0-9]*[_-][a-zA-Z0-9]*_` instead if you don't want the empty match https://ideone.com/guuvvD – The fourth bird Aug 11 '21 at 21:17
  • You have to drop the slashes `/` (and trailing `g`). Besides I don't get why you need the last `(.*?)` – Tranbi Aug 11 '21 at 21:19

0 Answers0