การค้นหา Pattern
คุณต้องจัดกลุ่มยอดขายตามรุ่นของแพลตฟอร์มเพื่อวิเคราะห์แนวโน้ม โดยการดึงหมายเลขเวอร์ชันออกจากชื่อแพลตฟอร์ม จะช่วยให้จัดหมวดหมู่ยอดขายตามรุ่นของคอนโซลได้ ชื่อแพลตฟอร์มบางส่วนได้ถูกดึงมาจากชุดข้อมูลวิดีโอเกมแล้ว
แพ็กเกจที่จำเป็น เช่น CharMatcher และ Pattern ได้รับการนำเข้าให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การทำความสะอาดข้อมูลใน 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");
}
}
}
}