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

My reg ex in python doesnt match using re.serach()

Im trying to extract the substring ‘$200’ from this text:
Autorización 28 MAY 2024, 20:34 (hora de CDMX)

$200.oo Monto

Concept

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

Im using the next method in a for:

monto = re.search('^[$][0-9]+', line in the text)

But it doesnt match

>Solution :

You have a ^ in your regexp, which anchors it to the start of the string. From the re manual:

^
(Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline.

That’s naturally no good if you want to find something in the middle of a string (unless you do actually want to find matches at new lines in the string, then set the flags=re.MULTILINE option, as shown above).

Try

re.search(r'[$][0-9]+', ...)

instead (so no anchoring).

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