David Kaplan
David Kaplan

Reputation: 150

Java multiple split - keep delimiters

I know similar questions have been asked but I havent been able to find the specific thing I am looking for involving square brackets.

The following is input:

[NAME] Bob
[NUMBER] 12456703
[STREET] Anything [UNIT] 32 [SUBURB] Benmore

I want to be able to input this data into a hasmap where the text in brackets will be the key and the info not in brackets will be the data. I have been trying to use substring and split but I got into problems when there was more than one set of brackets per line?
Thanks alot

Upvotes: 2

Views: 1111

Answers (4)

FailedDev
FailedDev

Reputation: 26930

Try this:

 HashMap<String, String> map = new HashMap<String, String>(); 
 Pattern regex = Pattern.compile("(\\[\\w+\\])\\s+(\\w+)");
 Matcher regexMatcher = regex.matcher(subjectString);
 while (regexMatcher.find()) {
     // matched text: regexMatcher.group(i)
     // add group 1 to key and group 2 to value of hash
     map.put(regexMatcher.group(1), regexMatcher.group(2));
 } 

Upvotes: 2

Goran Jovic
Goran Jovic

Reputation: 9508

If performance isn't a big problem (i.e. the size of input files is not too large), you can do a:

input = input.replaceAll("\\[","\n\\[")

to make your input one bracket per line, thus reducing the problem to the one you already solved.

Upvotes: 3

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39981

Something like this will work.

package se.wederbrand.stackoverflow.arraysorter;

import java.util.HashMap;
import java.util.Scanner;

public class ArraySorter {
    public static void main(String[] args) {
        String testString = "[NAME] Bob\n" +
                "[NUMBER] 12456703\n" +
                "[STREET] Anything [UNIT] 32 [SUBURB] Benmore ";

        HashMap<String, String> map = new HashMap<String, String>();

        Scanner scanner = new Scanner(testString);
        // this sets the delimiter to either [ or ]
        scanner.useDelimiter("[\\[\\]]");
        while (scanner.hasNext()) {
            String key = scanner.next();
            String value = scanner.next();

            // trim() removes any extra space or line breaks and leaves a clean string
            map.put(key.trim(), value.trim());
        }
    }
}

Upvotes: 3

Mechkov
Mechkov

Reputation: 4324

One idea (despite it is less efficient than using regex) is to split your data on empty space per line (if indeed your data has empty space between the key and values). You will most likely end up with an array of some sort.

Then, check to see if a token has an opening bracket, and if so you put it as a HashMap key. Otherwise, it is a value.

PriorityQueue

might be a good collection to store these values because you can peek() and poll() the data.

Upvotes: 1

Related Questions