开始使用免费开始使用

正则表达式

您在管理一家电子产品商店的库存时发现,部分商品名称包含特殊字符。正则表达式(regex)可以按模式搜索文本,从而移除这些特殊字符。请编写一个正则表达式模式,用于移除所有非大小写字母及空格的字符。

本练习是课程的一部分

Java 数据清洗

查看课程

练习说明

  • 完成正则表达式模式,使其排除大小写字母和空格。
  • 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);
    }
}
编辑并运行代码