使用正则表达式分隔类名类型

问题描述 投票:0回答:1

我有这样的输入数据:

TRectAdvancedVO
AccountDTO
SVGMindMapVO
TransferDTO
TreeViewDAO
UMLDTO
UniqueVO
TRectAdvanced
SVGConstants
TransferService
AccountController
MindMapDemo
SVGMindMapTool
TreeViewAdvancedTool
AccountService
TransferController
TreeViewTool
MindMapRectUtils
SVGUtils
TNodeAdvanced
TreeRectUtils
UMLSVGUtils
UniqueList

我应该按类型(Service、Controller、DAO、Utils、List、Tool...)分隔类的名称。

输出顺序并不重要。我该怎么做?

输出:

TRectAdvancedVO
SVGMindMapVO
UniqueVO

AccountDTO
TransferDTO
UMLDTO

TreeViewDAO

TRectAdvanced
TNodeAdvanced

SVGConstants

TransferService
AccountService

AccountController
TransferController

MindMapDemo

SVGMindMapTool
TreeViewAdvancedTool
TreeViewTool

MindMapRectUtils
SVGUtils
TreeRectUtils
UMLSVGUtils

UniqueList
python java python-3.x regex
1个回答
0
投票

这里有一个 Java 版本来完成您的任务:

String components = """
        TRectAdvancedVO
        AccountDTO
        SVGMindMapVO
        TransferDTO
        TreeViewDAO
        UMLDTO
        UniqueVO
        TRectAdvanced
        SVGConstants
        TransferService
        AccountController
        MindMapDemo
        SVGMindMapTool
        TreeViewAdvancedTool
        AccountService
        TransferController
        TreeViewTool
        MindMapRectUtils
        SVGUtils
        TNodeAdvanced
        TreeRectUtils
        UMLSVGUtils
        UniqueList
        UnknownDummy
        """;

List<String> types = List.of("VO", "DTO", "DAO", "Advanced", "Constants", "Service", "Controller", "Demo", "Tool", "Utils", "List");

Map<String, List<String>> groups = Arrays.stream(components.split("\\R"))
        .collect(Collectors.groupingBy(component -> types.stream()
                .filter(component::endsWith)
                .findFirst()
                .orElse("unknown")));

groups.forEach((groupName, groupMembers) -> {
    System.out.printf("Type: %s\n", groupName);
    groupMembers.forEach(component -> System.out.printf("\t%s\n", component));
    System.out.println();
});
© www.soinside.com 2019 - 2024. All rights reserved.