0

I am trying to detect if a sequence of text begins with a numbering, eg I), IV., a), C/, C. C -, C- etc with a regex. I tried the following regex :

m = re.search(r'^(IX?{\s|-|-\s|,|,\s|\)|\)\s|.|.\s}|IV?{\s|-\s|-|\)|\)\s|.\s|.}|VI{0,3}?{\s|-|-\s|\)|\)\s|.|.\s})', "II - Informations supplementaires")
m.group(0)
>> 'I'
m.group(1)
>> 'I'

It only gives me 'I' as a result, I don't have "II", "II -", which are expected .... What is my error in the pattern ^^ ?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
dada
  • 1,390
  • 2
  • 17
  • 40
  • Your pattern just does not match `II`, why do you say it is expected? – Wiktor Stribiżew Apr 24 '18 at 14:10
  • even with `m = re.search(r'^(IX?{\s|-|-\s|,|,\s|\)|\)\s|.|.\s}|IV?{\s|-\s|-|\)|\)\s|.\s|.}|VI{0,3}?{\s|-|-\s|\)|\)\s|.|.\s}|I{1,3})', "II - Informations supplementaires")` , "II" is not detected – dada Apr 24 '18 at 14:33
  • What are you trying to match? Your regex is really weird man... I don't think it's even close to what you want. Post a bunch of sample strings. Also, do you want `II` as output or `II - `? – ctwheels Apr 24 '18 at 14:38
  • I want both. I think this pattern is more efficient `"^(IX|IV|VI{0,3}|I{1,3})(\s|-|-\s|,|,\s|\)|\)\s|.|.\s)"` – dada Apr 24 '18 at 14:58
  • With `.` are you trying to match `.` literally? Also, it would **really** help if you posted multiple sample strings. – ctwheels Apr 24 '18 at 15:07
  • Which range of Roman numerals are you trying to match? – ctwheels Apr 24 '18 at 15:12

0 Answers0