You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java/Java8/9将指定字符串拆分为含数字与描述的对象列表

Java 8/9 实现字符串拆分为数字+描述的对象列表

需求说明

给定输入字符串:

"9 music recordings; Music files to download. 38 Providing access to music databases and to MP3 websites. 41 Organization of live musical events; Music publishing services; conducting music events; composing music for others; live entertainment production; live performances by musical bands; musical performances; music production; composing music; Operating a music recording studio."

需要将其拆分为包含number(数字)和description(描述)的对象列表,最终每个对象对应一组数字+描述的条目。

解决方案

首先定义用于存储结果的实体类:

public class MusicServiceEntry {
    private final int number;
    private final String description;

    public MusicServiceEntry(int number, String description) {
        this.number = number;
        this.description = description;
    }

    public int getNumber() {
        return number;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return String.format("number = %d, description = %s", number, description);
    }
}

然后编写拆分逻辑,利用正则表达式匹配条目,并通过Java 8的API完成解析:

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class EntryParser {
    public static void main(String[] args) {
        String input = "9 music recordings; Music files to download. 38 Providing access to music databases and to MP3 websites. 41 Organization of live musical events; Music publishing services; conducting music events; composing music for others; live entertainment production; live performances by musical bands; musical performances; music production; composing music; Operating a music recording studio.";

        // 正则匹配规则:捕获数字开头的完整条目
        Pattern entryPattern = Pattern.compile("(\\d+)\\s+(.*?)(?=\\s+\\d+|$)");
        Matcher matcher = entryPattern.matcher(input);

        // 将Matcher转换为Stream(Java 8兼容写法)
        Iterable<Matcher> matcherIterable = () -> matcher;
        List<MusicServiceEntry> entries = StreamSupport.stream(matcherIterable.spliterator(), false)
                .map(m -> {
                    int num = Integer.parseInt(m.group(1));
                    String desc = m.group(2).trim();
                    return new MusicServiceEntry(num, desc);
                })
                .collect(Collectors.toList());

        // 输出验证结果
        entries.forEach(System.out::println);
    }
}

代码说明

  1. 正则表达式解析:
    • (\\d+):捕获条目前的数字,作为number字段
    • \\s+:匹配数字与描述之间的空格
    • (.*?)(?=\\s+\\d+|$):非贪婪匹配描述内容,直到遇到下一个数字前的空格或字符串结尾,确保不会跨条目匹配
  2. Stream处理:
    • 将Matcher转换为Iterable后生成Stream,利用Java 8的Stream API完成对象的映射与收集
    • 每个匹配项被解析为MusicServiceEntry对象,最终收集为列表

运行代码后,输出结果与需求完全一致:

number = 9, description = music recordings; Music files to download.
number = 38, description = Providing access to music databases and to MP3 websites.
number = 41, description = Organization of live musical events; Music publishing services; conducting music events; composing music for others; live entertainment production; live performances by musical bands; musical performances; music production; composing music; Operating a music recording studio.

内容的提问来源于stack exchange,提问作者meet patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 23:15:35