2015-07-21 37 views
0

我有一些像這樣的模式的一些字符串數據: 「0x4F爲0xBB 0x74 0xA9」我的模式在哪裏不正確(Java中的正則表達式)?

  1. 它有四個部分。
  2. 每個部分有三個或四個字符。
  3. 每個部分以0x開頭。
  4. 在我們有空間的部分。
  5. 性格只是:A B C d電子網
  6. 編號:0-9

**我想回到這個模式爲字符串!如何退貨? 假設採樣輸入的字符串,我明白了,是: 「vhmbjfbfbbdbd這是我的字符串0x4F爲0xBB 0x74 0xA9的一部分,休息afhahjbsdvbskb」 如何匹配後返回字符串:「0x4F爲0xBB 0x74 0xA9」

String MP = MatchedPart(str).toString(); 
private static StringBuilder MatchedPart(String s) { 
    List<String> sarr = Arrays.asList(s.split(" 0x[A-F0-9]{1,2} 0x[A-F0-9]{1,2} 0x[A-F0-9]{1,2} 0x[A-F0-9]{1,2}")); 
    StringBuilder machpart = new StringBuilder(); 
    for(String t : sarr) { 
     machpart = machpart.append(t) ; 
    } 
    return machpart ; 
} 

我寫這個函數,得到了string和檢查模式:

public static boolean test (String s) { 

    Pattern pattern = Pattern.compile(" 0x[ABCDEF0-9] 0x[ABCDEF0-9] 0x[ABCDEF0-9] 0x[ABCDEF0-9]"); 
    Matcher matcher = pattern.matcher(s); 
    if (matcher.find()){ 
    return true; 
    } 
    return false; 
} 
+0

只有數字從1到9?有一個0是不可能的? –

+0

檢查[this](http://ideone.com/t3iWhp),它可能有幫助。 –

+0

@RubénJiménez,是的,正確的,我糾正了我的問題... – hossein

回答

0

根據您的問題,只是使用像如下:

public class PatternIncorrect { 

    public static void main(String[] args) { 
     System.out.println("------Test 1------------- "); 
     System.out.println(test1("0x4F 0xBB 0x74 0xA9")); 
     System.out.println("------Test 2--------------"); 
     System.out.println(test2("0x4F 0xBB 0x74 0xA9")); 
     System.out.println("------Test 3--------------"); 
     List<String> test3 = test3("0x4F 0xBB 0x74 0xA9asdasd0x4F 0xBB 0x74 0xA9mfdskf0x4F 0xBB 0x74 0xA9"); 
     for(String t : test3) { 
      System.out.println(t); 
     } 
    } 

    public static boolean test1(String str) { 
     List<String> strings = Arrays.asList(str.split(" ")); 
     boolean isMatch = true; 
     String regEx = "0x[1-9][ABCDEF]|0x[ABCDEF][1-9]|0x[1-9][1-9]|0x[ABCDEF][ABCDEF]"; 
     for (String s : strings) { 
      if (!s.matches(regEx)) { 
       System.out.println(s); 
       isMatch = false; 
       break; 
      } 
     } 
     return isMatch; 
    } 

    public static boolean test2(String s) { 
     String regEx = "0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2}"; 
     Pattern pattern = Pattern.compile(regEx); 
     Matcher matcher = pattern.matcher(s); 
     return matcher.find(); 
    } 

    public static List<String> test3(String s) { 
     String regEx = "0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2}"; 
     return Arrays.asList(s.split(regEx)); 
    } 
} 

源代碼在enter link description here

+0

坦克爲您的答案,浩從該字符串輸入返回此模式? – hossein

+0

如何獲得mached部分和返回字符串? – hossein

+0

只需將用戶輸入傳遞到基於模式匹配返回布爾值的方法之一即可。 – developerbhuwan

0

嘗試

Pattern pattern = Pattern.compile(" 0x[ABCDEF1-9]{1,2} 0x[ABCDEF1-9]{1,2} 0x[ABCDEF1-9]{1,2} 0x[ABCDEF1-9]{1,2}"); 

如果你想更熟悉Java中的正則表達式,請閱讀:https://docs.oracle.com/javase/tutorial/essential/regex/index.html

+0

是的,但正如它聲明「每個部分有3或4個字符」,我修改,使其工作如期 – Gordak

+0

坦克爲您的答案,浩返回這個模式從那個String輸入? – hossein

0

您的表達式以空白開頭,從我所看到的字符串開始,您的輸入字符串沒有。

而且,更重要的是,根據你的描述,你試圖匹配3個或4個字符,但根據你的表情,你總是期待3.

更換Pattern pattern = Pattern.compile(" 0x[ABCDEF1-9] 0x[ABCDEF1-9] 0x[ABCDEF1-9] 0x[ABCDEF1-9]");Pattern pattern = Pattern.compile("0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2} 0x[A-F1-9]{1,2}");,它應該工作。

有記錄的,0x00也應該是一個有效的十六進制數字,你的表達似乎不認爲是有效的。

0

您可以使用預定義模式\ p {} XDigit

替換[ABCDEF1-9]另一個建議:

Scanner sc = new Scanner(s); 
int count = 0; 
while(sc.hasNextInt(16)){ 
    sc.nextInt(16); 
    count++; 
} 
return count == 4;