简体   繁体   English

如何使用多个分隔符拆分多行?

[英]How do I split multiple lines with multiple delimeters?

I have a.txt file with multiple lines with many emails such as Mike@sport.member.com, Laura@music.member.com, Quinn@music.member.com .我有一个包含多行的.txt 文件,其中包含许多电子邮件,例如Mike@sport.member.com, Laura@music.member.com, Quinn@music.member.com How do I split them so I can add them to seperate arraylists like music or sport ?我如何拆分它们以便将它们添加到单独的数组列表中,例如musicsport

Mike: sport
Laura: music
Quinn: music

Thanks so much.非常感谢。

you can use regex你可以使用正则表达式

    Pattern pattern = Pattern.compile("(\\w+)@(\\w+).(\\w+).(\\w+)");
    Matcher matcher = pattern.matcher("Laura@music.member.com");

    if (matcher.find()) {
        matcher.group(1); //this return the name
        matcher.group(2); //this return music sport
        matcher.group(3); // get the company name
        matcher.group(4); // get the domain com in this case
    }

use this to fill the arraylist as you like and have a nice day:)用它来填写 arraylist,祝你有美好的一天:)

If you don't mind using ArrayUtils you can use this code:如果您不介意使用 ArrayUtils,您可以使用以下代码:

    String[] emails = new String[] {"Mike@sport.member.com", "Laura@music.member.com", "Quinn@music.member.com"};

    int startLength;
    String[][] split = new String[emails.length][];

    for(int i = 0; i < emails.length; i++) {
        split[i] = emails[i].split("@"); //split by @
        startLength = split[i].length;
        for(int j = 0; j < startLength; j++) {
            split[i] = ArrayUtils.addAll(split[i], split[i][0].split("\\.")); //Split by . and add items to the end of the array
            split[i] = ArrayUtils.remove(split[i], 0); //remove the first item of the array
        }
    }

    System.out.println(Arrays.deepToString(split));

Output: Output:

[[Mike, sport, member, com], [Laura, music, member, com], [Quinn, music, member, com]]

What this does is it uses.split("@") to split the emails into arrays separated by "@" Then goes through for array and splits each item in the array with.split(".").它的作用是使用 .split("@") 将电子邮件拆分为以“@”分隔的 arrays,然后遍历数组并使用 .split(".") 拆分数组中的每个项目。

As it splits the elements the second time it adds them to the end of the split array and removes the first item (which was the item that it just split up).当它第二次拆分元素时,它将它们添加到拆分数组的末尾并删除第一个项目(这是它刚刚拆分的项目)。

Note that I had to make the startLength variable to keep track of the original size of the split array because in the second loop its constantly changing length.请注意,我必须设置 startLength 变量来跟踪拆分数组的原始大小,因为在第二个循环中它的长度不断变化。

This gives you a 2D array with all the items but I won't blame you for no using this because its a bit of a mess.这为您提供了一个包含所有项目的二维数组,但我不会因为没有使用它而责怪您,因为它有点乱。

There are two steps to solve this problem.有两个步骤可以解决这个问题。

  1. Read lines from file(a.txt).从文件 (a.txt) 中读取行。
    public List<String> readFile() throws IOException {
        String fileName = "/Users/folder/a.txt"; 
        FileInputStream inputStream = new FileInputStream(fileName);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        String str = null;
        List<String> list = new ArrayList<>();
        while ((str = bufferedReader.readLine()) != null) {
            list.add(str);
        }
        //close
        inputStream.close();
        bufferedReader.close();
        return list;
    }
  1. Split line, take out data.分割线,取出数据。
    List<String> lines = readFile();
    Pattern pattern = Pattern.compile("([a-zA-Z]+)@([a-zA-Z]+)");
    for (String line : lines) {
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()){
            System.out.println(matcher.group(1) + ":" + matcher.group(2));
        }
    }
    

The result is结果是

Mike:sport
Laura:music
Quinn:music

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

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