开始使用免费开始使用

模式查找

您需要按平台世代对销量分组,以进行趋势分析。通过从平台名称中提取版本号,您可以将不同主机世代的销量进行分类。您已经从视频游戏数据集中抽取了一些平台名称。

所需的包(如 CharMatcherPattern)已为您导入。

本练习是课程的一部分

Java 数据清洗

查看课程

练习说明

  • 检查 matcher 是否能找到任何版本号。
  • 输出 matcher 找到的版本号。

交互式实操练习

通过完成这段示例代码来试试这个练习。

public class PatternValidation {
    public static void main(String[] args) {
        List platforms = Arrays.asList("PlayStation 5", "PlayStation 4", "Xbox One", "Switch");
        Pattern versionPattern = Pattern.compile("\\d");  // Match single digit versions

        for (String platform : platforms) {
            Matcher matcher = versionPattern.matcher(platform);
            // Check if the matcher finds any version number
            if (____.____()) {
                // Report the version number that the matcher finds
                System.out.println(platform + ": version " + ____.____());
            } else {
                System.out.println(platform + ": no version number");
            }
        }
    }
}
编辑并运行代码