算法基础——2.5正则表达式进阶

例一:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.   计算表达式的值(假设只有加法、减法、乘法) 
  3.   23+15-16*(25+(6-3)+9*2) 
  4. */  
  5.   
  6. import java.util.regex.*;  
  7.   
  8. public class T1  
  9. {  
  10.     // 计算没有括号的表达式  
  11.     static String calcuNoParen(String s)  
  12.     {  
  13.         if(s.length()<1return s;  
  14.           
  15.         // 先把所有乘法消除  
  16.         Pattern pat = Pattern.compile("([0-9]+)\\*([0-9]+)");   
  17.         while(true){  
  18.             Matcher mc = pat.matcher(s);  
  19.             if(mc.find()==falsebreak// 已经没有乘法了  
  20.               
  21.             int res = Integer.parseInt(mc.group(1)) * Integer.parseInt(mc.group(2));  
  22.             s = s.replace(mc.group(),res + "");  // 注意这里不能用replaceAll  
  23.         }  
  24.           
  25.         //再从左到右消除所有加法或减法  
  26.         pat = Pattern.compile("([0-9]+)([\\+\\-])([0-9]+)");   
  27.         while(true){  
  28.             Matcher mc = pat.matcher(s);  
  29.             if(mc.find()==falsebreak// 已经没有运算符了  
  30.             int res = 0;  
  31.             if(mc.group(2).equals("+"))  
  32.                 res = Integer.parseInt(mc.group(1)) + Integer.parseInt(mc.group(3));  
  33.             else  
  34.                 res = Integer.parseInt(mc.group(1)) - Integer.parseInt(mc.group(3));  
  35.             s = s.replace(mc.group(),res + "");  // 注意这里不能用replaceAll  
  36.         }  
  37.           
  38.         return s;     
  39.     }  
  40.       
  41.     // 计算可能含有括号的表达式  
  42.     static String calcu(String s)  
  43.     {  
  44.         s = s.replaceAll(" +",""); // 消除空格  
  45.         Pattern pat = Pattern.compile("\\(([^\\(\\)]*)\\)");   
  46.           
  47.         while(true){  
  48.             Matcher mc = pat.matcher(s);  
  49.             if(mc.find()==falsebreak;  
  50.             // 把括号内先计算,然后消除括号  
  51.             s = s.replace(mc.group(), calcuNoParen(mc.group(1)));    
  52.         }  
  53.           
  54.         return calcuNoParen(s);  
  55.                   
  56.     }  
  57.       
  58.     public static void main(String[] args)  
  59.     {  
  60.         /* 
  61.         String s = "23+15-16*(25+(6-3*2*1)+9*2)"; 
  62.          
  63.         //任务:找到最内层括号中的内容,计算后替换掉 
  64.  
  65.         */  
  66.           
  67.         System.out.println(calcu("5 + 3 * 2 * (2+2-3) - 1"));  
  68.           
  69.     }  
  70. }  
例二:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /*第02讲-正则表达式进阶_展开串 
  2.  
  3. 实际开发中,常常会出现用到一批文件,而这些文件的文件名有某种规律,我们在表述的时候,往往习惯简写,但这样程序又不好识别。 
  4. 比如: 
  5. c:/abc/xyz/k[11..19].dat 
  6. 实际表示的就是: 
  7. c:/abc/xyz/k11.dat 
  8. c:/abc/xyz/k12.dat 
  9. c:/abc/xyz/k13.dat 
  10. c:/abc/xyz/k14.dat 
  11. c:/abc/xyz/k15.dat 
  12. c:/abc/xyz/k16.dat 
  13. c:/abc/xyz/k17.dat 
  14. c:/abc/xyz/k18.dat 
  15. c:/abc/xyz/k19.dat 
  16. 本题目要求是:给定一个含有简写的串,要求展开为所有文件名。 
  17. 简写的格式为:[整数..整数]*/  
  18. import java.util.regex.*;  
  19. public class C5 {  
  20.     public static void main(String[] args) {  
  21.         String s = "c:/abc/xyz/k[11..19].dat";  
  22.         int begin = 0, end = 0;  
  23.           
  24.         Pattern pat = Pattern.compile("\\[([0-9]+)..([0-9]+)\\]");  
  25.         Matcher mc = pat.matcher(s);  
  26.         if(mc.find()){  
  27.             begin = Integer.parseInt(mc.group(1));//第一个文件号  
  28.             end = Integer.parseInt(mc.group(2));//最后一个文件号  
  29.             for(int i = begin; i <= end; ++i){  
  30.                 System.out.println(s.replaceAll("\\[([0-9]+)..([0-9]+)\\]", i+""));  
  31.             }  
  32.         }  
  33.     }  
  34. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值