请教一个正则表达式,替换所有除了字母和单个空格外的所有字符例如: Contents
PrefaceChapter 1: Getting started with the ASP.NET MVC Framework这段要替换为 Contents Preface Chapter Getting started with the ASP NET MVC Framework求解

解决方案 »

  1.   


    package com.pattern.test;import java.util.regex.Pattern;public class PatternTest { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Pattern pa=Pattern.compile("[\\W,\\d]+");
    String str="Chapter 1: Getting started with the ASP.NET MVC Framework";
    String[] s=pa.split(str);
    for(String st:s){
    System.out.println(st);
    }

    }}
      

  2.   


    package jeelon.regularExpression.Test;public class Base { public static void main(String[] args) {

    String yuanlaiString = "Contents " + "\n" +
    "Preface " +"\n" +
    "Chapter 1: Getting started with the ASP.NET MVC Framework" ;
    System.out.println("原来的:");
    System.out.println(yuanlaiString);

    System.out.println("替换过后的:");
    System.out.print(yuanlaiString.replaceAll("[^a-zA-Z,\\r]", " "));
    }
    }原来的:
    Contents 
    Preface 
    Chapter 1: Getting started with the ASP.NET MVC Framework
    替换过后的:
    Contents  Preface  Chapter  Getting started with the ASP NET MVC Framework
      

  3.   

    你这只能消除一行的,如果是多行例如
                  MEAP Edition
           Manning Early Access Program
              Copyright 2007 Manning PublicationsFor more information on this and other Manning titles go to
                       www.manning.com
    Contents
    Preface
    就没办法了
      

  4.   

    问一下 [^a-zA-Z,\\r] 这个逗号代表什么?
      

  5.   


    System.out.print(yuanlaiString.replaceAll("[^a-zA-Z,\\r+]", " "));具体问题 具体分析  呵呵  ...
      

  6.   

    javaxiaochouyu问一下 [^a-zA-Z,\\r] 这个逗号代表什么?
      

  7.   


    str = str.replaceAll("(?s)[^a-zA-Z]+"," ");