Java编程算法基础-活用字符串API-正则表达式

Java中对字符串的处理操作包括:

搜索  匹配 替换 分割

1.正则表达式初步

    1-1split()方法的使用,参数为正则表达式

        用途:分割字符串

 JDK:      

split

public String[] split(String regex)
 
 
根据给定 正则表达式的匹配拆分此字符串。

该方法的作用就像是使用给定的表达式和限制参数 0 来调用两参数 split 方法。因此,所得数组中不包括结尾空字符串。

例如,字符串 "boo:and:foo" 使用这些表达式可生成以下结果:

Regex结果
:{ "boo", "and", "foo" }
o{ "b", "", ":and:f" }

参数:
regex - 定界正则表达式
返回:
字符串数组,它是根据给定正则表达式的匹配拆分此字符串确定的 
 


public class Test14 {
	public static void main(String[] args){
	
	 String ss = "abc   dsadsa   sdas  sda asd";
	 //String[] s = ss.split(" {1,}");
	 String[] s = ss.split(" +");
	 
	 for(int i = 0; i < s.length;i++)
		 System.out.println(s[i]);
	 
	    
	}
}

结果:

abc
dsadsa
sdas
sda
asd

package NO2;

public class Test14 {
	public static void main(String[] args){
	
	 String ss = "abc+dsadsa+sdas+sda+asd";
	 //String[] s = ss.split(" {1,}");
	 //String[] s = ss.split("\\+");
	 String[] s = ss.split("\\+");
	 
	 for(int i = 0; i < s.length;i++)
		 System.out.println(s[i]);
	 
	  //System.out.println("\\+");  
	}
}
在正则表达式中表示分隔符"+", 需要表示成"\+"

而在java中"\"为转义符,故上面ss.spilit("\\+")参数为 "\\+"

    1-2.match函数  用途:匹配字符串(判断是否匹配正则表达式)

JDK:

matches

public boolean matches(String regex)
告知此字符串是否匹配给定的 正则表达式

调用此方法的 str.matches(regex) 形式与以下表达式产生的结果完全相同:

Pattern.matches( regex , str )

参数:
regex - 用来匹配此字符串的正则表达式
返回:
当且仅当此字符串匹配给定的正则表达式时,返回 true
public class Test15 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String ss = "AZ125";
		boolean flag = ss.matches("[A-Z]{1,2}[1-9]{1,5}"); //{1,2}分别为表示出现的最小,最大次数
                                                                     //+表示{1,}(最小1次,最多不限) *表示{0,} ?表示{0,1}
                                                                     //+ * ?在正则表达式中成为量词(不能直接表示它本身),如要在正则表达式中表示它本                                                                     //身,则需要转义
                                                                     //如"+" 应该表示为"\\+"
		System.out.println(flag);

	}

}
结果:true
        1-3 replaceAll   用途:替换

replaceAll

public String replaceAll(String regex,
                         String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的 正则表达式的子字符串。

调用此方法的 str.replaceAll(regex, repl) 形式与以下表达式产生的结果完全相同:

Pattern.compile( regex ).matcher( str ).replaceAll( repl )

注意,在替代字符串中使用反斜杠 (\) 和美元符号 ($) 与将其视为字面值替代字符串所得的结果可能不同;请参阅 Matcher.replaceAll。如有需要,可使用 Matcher.quoteReplacement(java.lang.String) 取消这些字符的特殊含义。

参数:
regex - 用来匹配此字符串的正则表达式
replacement - 用来替换每个匹配项的字符串
返回:
所得 String
public class Test16 {
	public static void main(String[] args) {
	
		
	String ss = "adasdasd   2015-9-12 33adsd dasdasdwe";	
	String strings = ss.replaceAll("[0-9]{4}-[0-9]{1}-[0-9]{2}","****");
	
	     System.out.println(strings);
	
	}

}
结果:adasdasd   **** 33adsd dasdasdwe

在正则表达式中,用括号括起来的部分称为 :子组

  

将 2015-9-12 替换为 9/12 2015年

<span style="font-size:14px;">public class Test16 {
	public static void main(String[] args) {
	
		
	String ss = "adasdasd   2015-9-12 33adsd dasdasdwe";	
	//String strings = ss.replaceAll("[0-9]{4}-[0-9]{1}-[0-9]{2}","****");//       9/12 2015年
	String strings = ss.replaceAll("([0-9]{4})-([0-9]{1})-([0-9]{2})", "$3/$2 $1年");
	     System.out.println(strings);
	
	}

}</span>
   1-4 replaceFirst 

replaceFirst

public String replaceFirst(String regex,
                           String replacement)
使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。

调用此方法的 str.replaceFirst(regex, repl) 形式与以下表达式产生的结果完全相同:

Pattern.compile( regex ).matcher( str ).replaceFirst( repl )

注意,在替代字符串中使用反斜杠 (\) 和美元符号 ($) 与将其视为字面值替代字符串所得的结果可能不同;请参阅 Matcher.replaceFirst(java.lang.String)。如有需要,可使用 Matcher.quoteReplacement(java.lang.String) 取消这些字符的特殊含义。

参数:
regex - 用来匹配此字符串的正则表达式
replacement - 用来替换第一个匹配项的字符串
返回:
所得 String









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值