Home > Back-end >  Remove leading dollar sign from data and improve current solution
Remove leading dollar sign from data and improve current solution

Time:03-19

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?

CodePudding user response:

You can use a regex with two capturing groups:

table\s ([^()]*\$([0-9] ))

See the regex demo. Details:

  • table - a word
  • \s - one or more whitespaces
  • ([^()]*\$([0-9] )) - Group 1:
    • [^()]* - zero or more chars other than ( and )
    • \$ - a $ char
    • ([0-9] ) - Group 2: one or more digits.

See the Python demo:

import re
text = "Job 1233:name_uuid (table n_Cars_1234567$20220316) done. Records: 24, with errors: 0."
rx = r"table\s ([^()]*\$([0-9] ))"
m = re.search(rx, text)
if m:
    print(m.group(1))
    print(m.group(2))

Output:

n_Cars_1234567$20220316
20220316

CodePudding user response:

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
  • Related