開始使用免費開始

Regular expressions

你正在管理一家電子產品商店的庫存,並注意到有些產品名稱含有特殊字元。正則表達式(regex)能用來在文字中搜尋樣式,移除這些特殊字元。請撰寫一個 regex 樣式,移除所有不是英文字母(大小寫)或空白的字元。

本練習屬於課程

使用 Java 進行資料清理

檢視課程

練習說明

  • 完成 regex 樣式,以排除小寫與大寫英文字母,以及空白。
  • messyProduct 中移除任何不是字母或空白的字元。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

public class ProductDataExample {
	public static void main(String[] args) {
    	String messyProduct = "Headphones *(Wireless)*";
        
        // Complete the regex pattern to exclude lower or upper case letters or spaces
        String regex = "[^____]";
        
        // Remove any character that is not a letter or space
        String cleanedProduct = ____.____(regex, "");
        System.out.println(cleanedProduct);
    }
}
編輯並執行程式碼