我有像这样的字符串1) "CAP07560" 2) "CAP41060" 3) "CAP43500"4) "CAP22390"我想首先删除"CAP"字符串中的文本,然后对于剩余的字符串,我想删除最后一个“0”并删除字符串开头的任何零。所以我的输出将是:1) "756" 2) "4106" 3) "4350"4) "2239"我尝试了以下方法-public class RegexTest {    public static void main(String[] args) {        // drop the last "0" and then drop any         // zeros at the start of the remaining number.         String str = "CAP07560";        String pattern = "^CAP[ 0]*|( |(?<!\\d))*$";        str = str.replaceAll(pattern, "");        System.out.println(str);    }}我的输出:7560所以我的输出是7560但它应该是756。
                    
                    
                1 回答
 
                    
                    
                            慕盖茨4494581
                            
                                
                            
                        
                        
                                                
                    TA贡献1850条经验 获得超11个赞
您可以使用交替在单次调用中执行此操作replaceAll:
str = str.replacAll("^CAP0*|0$", "");正则表达式详细信息:
- ^CAP0*: 匹配- CAP开头有 0 个或多个零
- |: 或者
- 0$:匹配最后一个零
添加回答
举报
0/150
	提交
		取消
	