Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regexp to find caret preceding integers not working

I’m trying to replace this [^19) Jones, Owen. with this [^19] Jones, Owen. using this regexp statement:

re.sub(r'(?<=\[\^[0-9])(\) )','] ', text)

But no replacement occurs and I’m unable to find the issue. Please help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to use

re.sub(r'(\[\^\d+)\) ', r'\1] ', text)

See the regex demo.

Details:

  • (\[\^\d+) – Group 1 (whose value is referred to with \1 from the replacement pattern): [^ and one or more digits
  • \) – a ) string.

Note you cannot use a lookbehind here, since \d+ matches a non-fixed amount of digits, and Python re lookbehind requires fixed-width patterns.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading