简体   繁体   中英

file parsing using tokens

I have a text file that reads as follows:

item1, description # brand1, brand2

item2, description # brand3

I have an array list of brands and another array lists of items.

My code reads and assigns everything until it gets to the "#" after the item description...

    while(input.hasNextLine())
        {
            String line = input.nextLine();
            String[] token = line.split("[,#]+");

            item = token[0];
            description = token[1];

            brand = token[2];

        }  

How can I get it to properly read what's after the "#" ?? I should mention that an item can belong to 2 or more brands

首先用“#”分割处理结果,然后用“,”分割。

I believe what you're looking for is regular expressions (regex)

It allows you set up a pattern that defines how the data is structured like this:

"(?<item>.+),\s(?<description>.+)\s\#\s(?<brands>.+)"

(disclaimer: just a quick example written by hand, might not work correctly) And then apply the pattern to a string using a matcher.

I'd strongly suggest you read up on regex, or take a tutorial on it. It's super useful. check out: http://docs.oracle.com/javase/tutorial/essential/regex/intro.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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