0

I am working on a project wherein I need to split the string below into multiple groups. I am using Named Regular expression to split into 3 groups:

  1. InvoiceNumber
  2. Description
  3. InvoiceAmount
671217            John Doe v Monica Geller, et al.            $55.50
                Pertains To: Joey Tribuiani                                                                               
                Firm's File # ABCDEF-123
                Claim No# 1237474

I am using the following regular expression:

search = '(?P<InvoiceNumber>\d+)\s+(?P<Description>.*?)\s+(?P<InvoiceAmount>.*?)\s+'
11
  • 1
    What exactly in the data is the InvoiceNumber, Description and InvoiceAmount? Commented Apr 8, 2020 at 13:38
  • InvoiceNumber:671217, InvoiceAmount:$55.50, Description:<Text in the middle> Commented Apr 8, 2020 at 13:40
  • Is the description the text before or after the InvoiceNumber? Commented Apr 8, 2020 at 13:41
  • 1
    Like this? (?P<InvoiceNumber>\d+)\s+.*?(?P<Description>\$\d+(?:\.\d+)?)\s+(?P<InvoiceAmount>.*(?:\r?\n.*)*) regex101.com/r/ly0Zjl/1 Commented Apr 8, 2020 at 13:59
  • 1
    @Kiran I have added an answer with a few adjustments. Commented Apr 8, 2020 at 14:18

1 Answer 1

1

You could use

(?P<InvoiceNumber>\d+)\s+.*?(?P<InvoiceAmount>\$\d+(?:\.\d+)?)\s+(?P<Description>\S[\S\s]*)

That will match

  • (?P<InvoiceNumber> Named group InvoiceNumber
    • \d+ Match 1+ digits
  • ) Close group
  • \s+.*? Match 1+ whitespace chars, then any char except a whitespace char non greedy
  • (?P<InvoiceAmount> Named group InvoiceAmount
    • \$\d+(?:\.\d+)? Match the invoice amount with an optional decimal part
  • ) Close group
  • \s+ Match 1+ whitespace chars
  • (?P<Description> Named group Description
    • \S[\S\s]* Match a non whitespace char so that there at least is a description and all chars that follow
  • ) Close group

Regex demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.