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

Remove leading dollar sign from data and improve current solution

I have string like so:

"Job 1233:name_uuid (table n_Cars_1234567$20220316) done. Records: 24, with errors: 0."

I’d like to retieve the datte from the table name, so far I use:

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

"\$[0-9]+"

but this yields $20220316. How do I get only the date, without $?

I’d also like to get the table name: n_Cars_1234567$20220316

So far I have this:

pattern_table_info = "\(([^\)]+)\)"
pattern_table_name = "(?<=table ).*"

table_info = re.search(pattern_table_info, message).group(1)
table = re.search(pattern_table_name, table_info).group(0)

However I’d like to have a more simpler solution, how can I improve this?

>Solution :

You can write a single pattern with 2 capture groups:

\(table (\w+\$(\d+))\)

The pattern matches:

  • \(table
  • ( Capture group 1
    • \w+\$ match 1+ word characters and $
    • (\d+) Capture group 2, match 1+ digits
  • ) Close group 1
  • \) Match )

See a Regex demo and a Python demo.

import re

s = "Job 1233:name_uuid (table n_Cars_1234567$20220316) done. Records: 24, with errors: 0."
m = re.search(r"\(table (\w+\$(\d+))\)", s)
if m:
    print(m.group(1))
    print(m.group(2))

Output

n_Cars_1234567$20220316
20220316
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