各位大哥:
       我想用正则表达式的匹配模式,问题如下:
       北京|上海|南京|
       这个是从数据库中取的,然后拼起来的字符串,长短不确定。
       假如我想用一句话:北京是中国首都。这句话来匹配上面的模式,就是说:把这句话中的“北京”匹配出来,应该怎么做?                                  小弟在次谢过了,感激涕零!!!

解决方案 »

  1.   

    SQL里可以获取第X个字到第N个字之间的字~你要是能确定就直接提取放到个变量里
    然后用的时候把 变量拼凑到要用的地方就行了.不知道 你要问的是不是这个意思啊`
    我也没太看明白你说的意思
      

  2.   

    package regex;import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class T1 { /**
     * @param args
     */
    public static void main(String[] args) {
    String reg="北京|上海";
    Pattern p=Pattern.compile(reg);
    Matcher m=p.matcher("北京是中国首都");
    while(m.find()){
    System.out.println(m.group());
    }
    }}
      

  3.   

    package regex;import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class T1 { /**
     * @param args
     */
    public static void main(String[] args) {
    String reg="北京|上海";
    Pattern p=Pattern.compile(reg);
    Matcher m=p.matcher("北京是中国首都");
    while(m.find()){
    System.out.println(m.group());
    }
    }}这一种我试过了,不行啊!
      

  4.   

    你要求都不说明。我认为 Areslp(努力ING)  正解,可以匹配出‘北京’。
      

  5.   

    got it!public class Test1 {
    public static void main(String[] args) {
    String s = "她说北京是中国首都。";
    System.out.println(s.replaceAll(".*(北京|上海|南京).*", "$1"));
    }
    }
      

  6.   

    public   static   void   main(String[]   args)   { 
    String   reg="北京 |上海|南京"; 
    Pattern   p=Pattern.compile(reg); 
    Matcher   m=p.matcher("北京和南京"); 
    while(m.find()){ 
    System.out.println(m.group()); 

    } }我想把上面的“北京”和“南京”都匹配出来,应该怎么做啊,上面的方法只能把 “北京”匹配出来》
           
                      
      

  7.   

    got   it! public   class   Test1   { 
    public   static   void   main(String[]   args)   { 
    String   s   =   "她说北京是中国首都。"; 
    System.out.println(s.replaceAll(".*(北京 ¦上海 ¦南京).*",   "$1")); 

    }
    =========================================================
    学习
      

  8.   

    while(m.find()){  
    System.out.println(m.group());  
    }  那是不是这一句有问题啊,我用这一句就只能出来一个。
      

  9.   

    String reg="北京|上海"; 
    Pattern p=Pattern.compile(reg); 
    Matcher m=p.matcher("北京是中国首都"); 
    while(m.find()){ 
    System.out.println(m.group()); 
    } 这样?
      

  10.   

            String reg="北京|上海|南京"; 
            Pattern p=Pattern.compile(reg); 
            Matcher m=p.matcher("北京是中国首都"); 
            while(m.find()){ 
                System.out.println(m.group()); 
            } 
      

  11.   

            String reg="北京|上海|南京"; 
            Pattern p=Pattern.compile(reg); 
            Matcher m=p.matcher("北京和南京"); 
            while(m.find()){ 
                System.out.println(m.group()); 
            }         我想问的是,怎么把 北京 和  南京 都匹配出来,我只能把 北京 匹配出来。
             请高手指点,在线等,谢谢了!!!
      

  12.   

            String reg="北京|上海|南京";  
            Pattern p=Pattern.compile(reg);  
            Matcher m=p.matcher("北京和南京");  
            while(m.find()){  
                System.out.println(m.group());  
            }          我想问的是,怎么把 北京 和  南京 都匹配出来,我只能把 北京 匹配出来。 
             请高手指点,在线等,谢谢了!!!
      

  13.   

     String reg="北京|上海|南京"; Pattern p=Pattern.compile(reg); Matcher m=p.matcher("北京和南京"); while(m.find()){ System.out.println(m.group()); } 
    不能吗???
      

  14.   


    String[] citys = "北京|上海|南京".split("\\|");
    for(String city: citys)
    System.out.println(city);
      

  15.   

      String reg="北京|上海|南京";  
            Pattern p=Pattern.compile(reg);  
            Matcher m=p.matcher("北京和南京");  
            while(m.find()){  
                System.out.println(m.group());  
            } ------------------
    这个不是已经可以了吗?