简体   繁体   English

Java多重拆分 - 保留分隔符

[英]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 [NAME]鲍勃
[NUMBER] 12456703 [号码] 12456703
[STREET] Anything [UNIT] 32 [SUBURB] Benmore [STREET]任何[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. 我希望能够将这些数据输入到hasmap中,括号中的文本将成为键,括号中的信息将是数据。 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 非常感谢

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());
        }
    }
}

If performance isn't a big problem (ie 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. 使您的输入每行一个支架,从而将问题减少到您已经解决的问题。

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));
 } 

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. 然后,检查令牌是否具有左括号,如果是,则将其作为HashMap键。 Otherwise, it is a value. 否则,它是一个值。

PriorityQueue

might be a good collection to store these values because you can peek() and poll() the data. 可能是存储这些值的好集合,因为您可以peek()poll()数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM