Question

I have a string in an inconvenient format. Here is an example:

(Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24)

I need to go through this string and extract only the first items in the parenthesis. So using the snippet from above as input, I would like my code to return:

Air Fresheners
Chocolate Chips
Juice-Frozen

Note that some of the items have - in the name of the item. These should be kept and included in the final output. I was trying to use:

Scanner.useDelimiter(insert regex here)

...but I am not having any luck. Other methods of accomplishing the task are fine, but please keep it relatively simple.

Was it helpful?

Solution 2

Try this one

  • Use regex to split on the basis of )->(

    String s="(Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24)";
    
    Pattern regex = Pattern.compile("\\)->\\(");
    Matcher regexMatcher = regex.matcher(s);
    int i=0;
    while (regexMatcher.find()) {
        System.out.println(s.substring(i+1,regexMatcher.start()));
        i=regexMatcher.end()-1;
    }
    System.out.println(s.substring(i+1,s.length()-1));
    
  • Try String.split() method

    String s = "(Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24)";
    
    for (String str : s.substring(1, s.length() - 1).split("\\)->\\(")) {
        System.out.println(str);
    }
    

OTHER TIPS

I know this is old and I'm no expert but can't you use replaceAll? As below:

String s = "(Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24)".replaceAll("(->)|[\\(\\)]|\\d+","");

for (String str : s.split(","))
{
  System.out.println(str);
}

This can be done with regular expressions. Where we match ([^,)(]*) matches any name that do not contain brackets or commas, ,\\d+\\) matches the ,14) part and (?:->)? matches possible -> after the tuple. We use group(1) to get the name (group(0) returns the whole tuple (Air Fresheners,17)->

    List<String> ans = new ArrayList<>();

    Matcher m = Pattern.compile("\\(([^,)(]*),\\d+\\)(?:->)?").matcher(str);
    while(m.find()){
        String s = m.group(1);
        ans.add(m.group(1));
    }

Given (Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24), this program returns [Air Fresheners, Chocolate Chips, Juice-Frozen]

(Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24)

You could think of it as everything between the ( and the ,

So,

\(.*?\,

would match "(Air Fresheners," (the ? is to make it non-greedy, and stop when it sees a comma)

So if you're keen to use regex, then just match these, and take a substring to get rid of the ( and ,

I would first go through using )-> as the delimiter. On each scanner.next() get rid of the first character (the parenthesis) using substring, and then place a second scanner on that string that uses , as the delimiter. In code this would look something like:

    Scanner s1 = new Scanner(string).useDelimiter("\\s*)->\\s*");
    while(s1.hasNext())
    {
        Scanner s2 = new Scanner(s1.next).useDelimiter("\\s*,\\s*");
        System.out.println(s2.next.substring(1));
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top