Ad

Remove Leading Dollar Sign From Data And Improve Current Solution

- 1 answer

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:

"\$[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?

Ad

Answer

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
Ad
source: stackoverflow.com
Ad