Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8996143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:42:53+00:00 2026-06-15T23:42:53+00:00

I am using regular expressions to get each line item’s data from a receipt.

  • 0

I am using regular expressions to get each line item’s data from a receipt.
The receipts are going to look like this:

Qty Desc
1   JD *#
    MARTINI *#   
2   XXXXXX 
3   YYYYYY
4   JD
    PEPSI *#

All items have quantities and descriptions, and some of them have an extra *#. Also, note that the descriptions can have spaces in them, and even more than one line, each line being able to have its own *#. I want to catch the quantity and description (if more than one line, get all lines), and I do not care at all about the extra *#. So in this example, for the first line item I would catch Quantity=1 and Description=”JD MARTINI”. For the fourth, Quantity=4 and Description=”JD PEPSI”.

My current regular expression looks like this:

((\d+)\s+(.*)(\s+\*#)?)

It is not working, and I assume it is because making the last parenthesis optional allows the greedy (.*) to catch absolutely everything. If the last parenthesis wasn’t optional, the regular expression would do its job for the line items with the extra *#, but it wouldn’t match the first and third one (because they don’t have the extra *#).

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:42:54+00:00Added an answer on June 15, 2026 at 11:42 pm

    After reading your modified question, I have determined that what you wish to accomplish cannot be done with one regular expression. You will have to do a combination of regex match + replace. (see this question: Regular expression to skip character in capture group)

    Match Regex: (\d+)\s+([A-Z\s*#]*[A-Z]+)

    Replace Regex: (*#(\s*))|(\r\n\s+)(?=\s)

    The match regex will match the quantity and the item description, including any in-between line breaks or *# occurrences, leaving out the final *#. I am assuming the last character in a description is a letter.

    After you run the match regex, you will get an array of matches back out, which you will need to iterate through to turn into objects. I wrote some handy code to do that for you. For each object, you will run the replace regex on the object’s description, which will remove the extraneous spaces and *#.

         class ReceiptItem
        {
            public int Quantity { get; set; }
            public string Description { get; set; }
    
            public override string ToString()
            {
                return string.Format("{0}\t{1}", Quantity, Description);
            }
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            var matches = Regex.Matches(textBox1.Text, @"(\d+)\s+([A-Z\s\*\#]*[A-Z]+)", RegexOptions.Multiline);
            var items = (from Match m in matches
                         select new ReceiptItem()
                                    {
                                        Quantity = int.Parse(m.Groups[1].Value),
                                        Description = Regex.Replace(m.Groups[2].Value, @"(\*\#(\s*))|(\r\n\s+)(?=\s)", "")
                                    });
    
            listBox1.Items.AddRange(items.ToArray());
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to parse the string {'result':(Boolean, MessageString)} using Python regular expressions to get Boolean
I'm trying to get file contents, replace some parts of it using regular expressions
How do I pull out the filename from a full path using regular expressions
I'm having some trouble using regular expression to get date in a string. Example
How can I get content of a DIV using regular expression. What I need
Using regular expressions in C#, is there any way to find and remove duplicate
Using regular expressions in .NET with the pattern ^%[^%]+%\Z and the string few)few% I
I'm using regular expressions with a python framework to pad a specific number in
I am using regular expressions to validate user input. The following code collects a
I'm currently using regular expressions to search through RSS feeds to find if certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.