MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap
Revision 2 as of 2014-08-05 22:18:29
  • Java

Java

Regular expressions

   1 /*
   2 javac TesteRe.java 
   3 java -cp . TesteRe 
   4 */
   5 /*
   6 gcj -C TesteRe.java  # compile with gcj, GNU compiler for Java
   7 gij -cp . TesteRe # run with gij, GNU interpreter for Java
   8 */
   9 
  10 import java.util.regex.Pattern;
  11 import java.util.regex.Matcher;
  12 
  13 public class TesteRe{
  14 
  15     public static void main(String args[]){ 
  16         String values[]={"aa12.txt","a123sss.txt","bs11bb.txt","123aaaa.sql","a12.txt","aaaa12","20ghj","6657"};
  17         Pattern a = Pattern.compile("^(\\D+)(\\d+)(\\D+)$");
  18         Pattern b = Pattern.compile("^(\\d+)(\\D+)$");
  19         Pattern c = Pattern.compile("^(\\D+)(\\d+)$");
  20         Pattern d = Pattern.compile("^(\\d+)$");
  21 
  22         for(String item:values){
  23             Matcher ma = a.matcher(item);
  24             Matcher mb = b.matcher(item);
  25             Matcher mc = c.matcher(item);
  26             Matcher md = d.matcher(item);
  27 
  28            if(ma.matches()){ 
  29                int val = Integer.parseInt(ma.group(2)) + 1;
  30                System.out.println(String.format("A: mv %s %s%d%s",item,ma.group(1),val,ma.group(3)  ) );
  31            }
  32 
  33            if(mb.matches()){ 
  34                int val = Integer.parseInt(mb.group(1)) + 1;
  35                System.out.println(String.format("B: mv %s %d%s",item, val , mb.group(2)  ) );
  36            }
  37 
  38            if(mc.matches()){ 
  39                int val = Integer.parseInt(mc.group(2)) + 1;
  40                System.out.println(String.format("C: mv %s %s%d",item,mc.group(1),val   ) );
  41            }
  42 
  43            if(md.matches()){ 
  44                int val = Integer.parseInt(md.group(1)) + 1;
  45                System.out.println(String.format("D: mv %s %d",item, val  ) );
  46            }
  47 
  48         }
  49     }
  50 }
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01