例如:
Contents
Preface
Chapter 1: Getting started with the ASP.NET MVC Framework
这段要替换为
Contents Preface Chapter Getting started with the ASP NET MVC Framework
求解
13 个解决方案
#1
那个空格数还是要有个限制的吧。
#2
是的,就是用正则表达式过滤后就只剩 “单词空格单词” 这样的形式的字符串
#3
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);
}
}
}
#4
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
#5
你这只能消除一行的,如果是多行
例如
MEAP Edition
Manning Early Access Program
Copyright 2007 Manning Publications
For more information on this and other Manning titles go to
www.manning.com
Contents
Preface
就没办法了
例如
MEAP Edition
Manning Early Access Program
Copyright 2007 Manning Publications
For more information on this and other Manning titles go to
www.manning.com
Contents
Preface
就没办法了
#6
问一下 [^a-zA-Z,\\r] 这个逗号代表什么?
#7
System.out.print(yuanlaiString.replaceAll("[^a-zA-Z,\\r+]", " "));
具体问题 具体分析 呵呵 ...
#8
javaxiaochouyu问一下 [^a-zA-Z,\\r] 这个逗号代表什么?
#9
楼主是不是换行符也不要替换掉?
#10
需要替换换行符啊
#11
str = str.replaceAll("(?s)[^a-zA-Z]+"," ");
#12
这个正解
默认情况是不匹配行结束符,加上(?s)表示这个表达式需要匹配行结束符。
#13
不错,嘿嘿 学习了
#1
那个空格数还是要有个限制的吧。
#2
是的,就是用正则表达式过滤后就只剩 “单词空格单词” 这样的形式的字符串
#3
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);
}
}
}
#4
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
#5
你这只能消除一行的,如果是多行
例如
MEAP Edition
Manning Early Access Program
Copyright 2007 Manning Publications
For more information on this and other Manning titles go to
www.manning.com
Contents
Preface
就没办法了
例如
MEAP Edition
Manning Early Access Program
Copyright 2007 Manning Publications
For more information on this and other Manning titles go to
www.manning.com
Contents
Preface
就没办法了
#6
问一下 [^a-zA-Z,\\r] 这个逗号代表什么?
#7
System.out.print(yuanlaiString.replaceAll("[^a-zA-Z,\\r+]", " "));
具体问题 具体分析 呵呵 ...
#8
javaxiaochouyu问一下 [^a-zA-Z,\\r] 这个逗号代表什么?
#9
楼主是不是换行符也不要替换掉?
#10
需要替换换行符啊
#11
str = str.replaceAll("(?s)[^a-zA-Z]+"," ");
#12
这个正解
默认情况是不匹配行结束符,加上(?s)表示这个表达式需要匹配行结束符。
#13
不错,嘿嘿 学习了