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 7573853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:17:12+00:00 2026-05-30T16:17:12+00:00

Compare how you would accomplish the two tasks mentioned below with and without regular

  • 0

Compare how you would accomplish the two tasks mentioned below with and without regular expressions. The problem:

The format for an SMS-based food delivery will be:

PABUSOG slash or comma repeated an infinite number of times @

// The quantity can only be numeric. For simplicity, assume that quantity is always an integer

e.g. PABUSOG STRFRY_SMAI/2 HSHBRWN_BRGR/1 COFEEFLT/1 @En311

it will capture the following:

STRFRY_SMAI – 2
HSHBRWN_BRGR – 1
COFEEFLT – 1

this is my sample code: // doing with regex

String message = "PABUSOG ASD_ASD/1 ASD_ASA/2";

    Pattern pattern = Pattern.compile("PABUSOG(\\s+([A-Z]+_[A-Z]+)(/|,)([0-9]))+"
            ,Pattern.CASE_INSENSITIVE);

    Matcher m = pattern.matcher(message);

    try
    {
        if (m.matches())
        {

            String food = m.group(2);
            String quantity = m.group(4);

             System.out.println(food + " -- " + quantity + "\\n");


        }
    }
    catch (NullPointerException e)
    {
    }

it displays the ASD_ASA — 2, it overrides the 1st one which is ASD_ASD/1.

it must display

ASD_ASD — 1

ASD_ASA — 2

  • 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-05-30T16:17:14+00:00Added an answer on May 30, 2026 at 4:17 pm

    You cannot accomplish that with a single regex giving you all the data inside groups. And there’s no great need for complex regex either. But still if you prefer regex try searching for pattern iteratively.

    if (!message.startsWith("PABUSOG")) {
        return;
    }
    
    Pattern pattern = Pattern.compile("([A-Z_]+)[/,]([0-9])+", Pattern.CASE_INSENSITIVE);
    
    Matcher m = pattern.matcher(message);
    while (m.find()) {
        String food = m.group(1);
        String quantity = m.group(2);
    
        System.out.println(food + " -- " + quantity);
    }
    

    Without complex regex you can do the following by using String API:

    // Check for correct header
    if (!message.startsWith("PABUSOG")) {
        return;
    }
    
    // split by whitespaces
    String[] items = message.split("\\s+");
    // skip header and iterate over remaining items
    for (String item : Arrays.asList(items).subList(1, items.length)) {
        // split each item by / or ,
        String[] foodQuantity = item.split("[/,]");
        assert foodQuantity.length == 2;
    
        String food = foodQuantity[0];
        String quantity = foodQuantity[1];
    
        System.out.println(food + " -- " + quantity);
    }
    

    To skip items started with @ you can either add

    if (item.startsWith("@")) {
        break; // or continue if it can be not the last
    }
    

    inside loop or limit subList in the following way if you sure that such item is always present and terminates the sequence: Arrays.asList(items).subList(1, items.length - 1).

    By the way, your pattern [A-Z]+_[A-Z]+ won’t match COFEEFLT from your example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to compare two dates in javascript. I have been doing some
I would like to compare two tables and then update if some logic is
What would be a better approach to compare two floating point values - using
I would like to be able to compare two classes derived from the same
I would like to know if there is anyway I can compare two columns
I have a logical condition that does not work. it would compare two characters,
On my website I would like to compare two companies with an image-chart/-graph. Each
I would like to compare a screenshot of one application (could be a Web
I would like to compare the contents of a couple of collections in my
I would like to compare 4 character string using wildcards. For example: std::string wildcards[]=

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.