2016-08-12 73 views
0

我得到一個包含逗號分隔數據的csv文件。某些數據可能包含Excel單元數等B1,B2,C1,它表示的MS Excel細胞數CSV數據Java正則表達式+如何在字符串中找到匹配模式

b1的

實施例,2 3 4 B1,5 C2 3 D2,5 4,2 e1

我需要確定是否有任何csv數據包含像a1或c1或b1這樣的數據。

即我需要找到數據是否包含一個字符後跟一個數字。

我已經使用JAVA正則表達式編寫了下面的程序。

雖然這在數據只包含b1或c1時有效,但當數據在其之前或之後包含更多charachter時,它無法找到b1或c1。

例如

實施例1級的作品和打印true

package com.test; 

public class PatternTest { 

    public static void main(String[] args) { 
     String pattern = "(([A-Za-z].*[0-9]))"; 

     String data = "b2"; 
     if(data.matches(pattern)){ 
      System.out.println("true"); 
     }else{ 
      System.out.println("false"); 
     } 

    } 

} 

實施例2不工作和打印假。我怎樣才能讓例子2工作,以便能找到B1或C1或者A1或從之前中含有較多的charachters字符串中的A2和後

package com.test; 

public class PatternTest { 

    public static void main(String[] args) { 
     String pattern = "(([A-Za-z].*[0-9]))"; 

     String data = "1 b2 3 4 "; 
     if(data.matches(pattern)){ 
      System.out.println("true"); 
     }else{ 
      System.out.println("false"); 
     } 

    } 

} 

回答

0

請忽略。我找到了解決辦法如下圖所示

package com.test; 

public class PatternTest { 

    public static void main(String[] args) { 
     String pattern = "((.*[A-Za-z].*[0-9].*))"; 

     String data = "c2 3 c2 *"; 
     if(data.matches(pattern)){ 
      System.out.println("true"); 
     }else{ 
      System.out.println("false"); 
     } 



    } 

} 
0

你可以這樣說:

String str = "b1, 2 3 4 b1, 5 c2 3 d2, 5 4, 2 e1"; 
if (str.matches(".*\\p{Alpha}\\d.*")) { 
    System.out.println(true); 
} else { 
    System.out.println(false); 
} 
// Result is true for current str 

在你的情況你自己回答這將是true也爲這些字符串B22,B & 2,cc3等你不想要的。

相關問題