正则式:找出字符串中第一个以”S“打头,到”E“结尾的子字符串,并替换成”ABC“。

找出字符串中第一个以”S“打头,到”E“结尾的子字符串,并替换成”ABC“。

 

代码:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

 

public class SimpleTest {
 public static void main(String[] args) throws Exception {
  String str = "S123E456E7890";
  Pattern p = Pattern.compile("S.*?E");
  Matcher m = p.matcher(str);
  if (m.find()) {
   String findResult = m.group(0);
   System.out.println("Find one:" + findResult);
  
   String replaceResult = m.replaceFirst("ABC");
   System.out.println("Replace result:" + replaceResult);
  }
 }
}

 

说明:

S.*?E,”S“表示以S打头,”E“表示以E结尾,”.*“表示中间可以是任意字符,?表示找到第一个E即查找结束。

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值