2017-02-18 17 views
0

我是java regex的新手。所以,我有一個包含不同節點的xml文件。文件是 -如何通過考慮上一個和下一個單詞來精確匹配字符串

<Node id="855"/>PROFILE<Node id="862"/>:<Node id="863"/> 
<Node id="864"/>8<Node id="865"/> <Node id="866"/>years<Node id="871"/> <Node id="872"/>IT<Node id="874"/> <Node id="875"/>industry<Node id="883"/> <Node id="884"/>experience<Node id="894"/> <Node id="895"/>in<Node id="897"/> <Node id="898"/>web<Node id="901"/> <Node id="902"/>based<Node id="907"/> <Node id="908"/>applications<Node id="920"/> <Node id="921"/>that<Node id="925"/> <Node id="926"/>involved<Node id="934"/> <Node id="935"/>extensive<Node id="944"/> <Node id="945"/>development<Node id="956"/> <Node id="957"/>work<Node id="961"/> <Node id="962"/>in<Node id="964"/> <Node id="965"/>Java<Node id="969"/>/<Node id="970"/>J<Node id="971"/>2<Node id="972"/>EE<Node id="974"/>,<Node id="975"/>Jquery<Node id="981"/>,<Node id="982"/>Jqgrid<Node id="988"/>,<Node id="989"/>Ajax<Node id="993"/>.<Node id="994"/> 
<Node id="995"/>Good<Node id="999"/> <Node id="1000"/>experience<Node id="1010"/> <Node id="1011"/>in<Node id="1013"/> <Node id="1014"/>agile<Node id="1019"/> <Node id="1020"/>methodology<Node id="1031"/> <Node id="1032"/>.<Node id="1033"/> 

我有一個字符串,我需要匹配這個字符串。

PROFILE: 
8 years IT industry experience in web based applications that involved extensive development work in Java/J2EE,Jquery,Jqgrid,Ajax. 

所以,

private void parseXml(ArrayList<String> elements, String filePath) { 
    boolean flag = false; 
    String nextId = "0"; 
    String xmlData = getTextWithNodesDataFromXml(filePath); 
    for (String s : elements) { 
     System.out.println(s); 
     String token; 
     int id; 
     String regex = ""; 
     if (flag == false) { 
      regex = "<Node id=\"([0-9]+)\"\\/>(" + s + ")"; 
      flag = true; 
      Pattern pattern1 = Pattern.compile(regex); 
      Matcher matcher1 = pattern1.matcher(xmlData); 
      if (matcher1.find()) { 
       System.out.println("match found -->" + s); 
      } 
     } 

SO第一個參數是包含要被匹配和第二是該文件的路徑的字符串令牌的陣列列表。 xmlData與我前面提到的節點有關,我需要與之匹配。所以如果我發現PROFILE匹配三次,那我該如何檢查整個字符串?我必須匹配確切的字符串與這個節點?我怎樣才能做到這一點?

+0

我在標題中的「取一個和下一個字考慮到」混淆。也通過使用詞「簡介」。你可以添加幾個不同的輸入並說出你期望的輸出嗎?如果匹配,或者在節點文件中找到句子(元素)開始的位置,您是返回true/false嗎? –

+0

是的,我想從句子開始和結束的地方找到節點的id。基本上,我想要開始和結束抵消。 – ganeshk

+0

@hack_on我的意思是這個文件可以很多次包含配置文件。但是,在配置文件剩餘字符串應該是相同的。因此,如果我們發現配置文件三次,並在第一個配置文件後的下一件事情是不是:和哪些是別的,所以,我想精確匹配整個字符串。 – ganeshk

回答

0

我建議比較類似的東西:你顯示的XML是句子的標記。您嘗試將其與整個字符串進行比較。

如果將XML轉換爲字符串數組並將標記語句PROFILE標記爲String.split("\\s+"),則必須比較兩個字符串數組。

也許,完全匹配不是你想要的嗎?

在這種情況下,您必須計算相似比例p,決定閾值t並僅保留PROFILE p > t

0

我已經做了一些關於正在請求什麼的假設,並創建了一個工作解決方案,因爲我可以最好地理解它。

我通過避免讀取文件來欺騙我。

package stacktest; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class StringMatch 
{ 
    private String getTextWithNodesDataFromXml(String filePath) 
    { 
     // cheating here by passing the xml as a string in. 
     return filePath; 
    } 

    private int [] findMatches(List<String> elements, String filePath) 
    { 
     String xmlData = getTextWithNodesDataFromXml(filePath); 

     String outerRegex = "(<Node id=\"[0-9]+\"\\/>PROFILE<Node id=\"[0-9]+\"\\/>:<)"; 
     Pattern outerPattern = Pattern.compile(outerRegex, Pattern.DOTALL); 
     Matcher outerMatcher = outerPattern.matcher(xmlData); 
     int outerMatches = 0; 
     boolean first = true; 
     int lastStart=0; 
     ArrayList<String> profiles = new ArrayList<String>(); 
     while (outerMatcher.find()) 
     { 
      String localXML = outerMatcher.group(1); 
      int startIndex = outerMatcher.start(1); 

      if (!first) 
      { 
       localXML = xmlData.substring(lastStart, startIndex); 
       profiles.add(localXML); 
      } 
      lastStart = startIndex; 
      first = false; 
      outerMatches++; 
     } 
     // Is there a hanging one at the end? 
     if (outerMatches > 0) 
     { 
      String localXML = xmlData.substring(lastStart); 
      profiles.add(localXML); 
     } 

     for (String profile: profiles) 
     { 
      // System.out.println(localXML); 
      String regex = "<Node id=\"([0-9]+)\"\\/>([^<]+)"; 

      Pattern pattern1 = Pattern.compile(regex); 
      Matcher matcher1 = pattern1.matcher(profile); 
      ArrayList<String> toMatch = new ArrayList<String>(); 
      ArrayList<String> idMatch = new ArrayList<String>(); 

      while (matcher1.find()) 
      { 
       String token = matcher1.group(2); 
       toMatch.add(token); 
       String id = matcher1.group(1); 
       idMatch.add(id); 
       outerMatches++; 
      } 

      if (elements.size() == toMatch.size()) 
      { 
       boolean didFind = true; 
       for (int i=0; i< elements.size(); i++) 
       { 
        String element = elements.get(i); 
        String match = toMatch.get(i); 
        if (!element.equals(match)) 
        { 
         didFind = false; 
        } 
       } 

       if (didFind) 
       { 
        int[] toReturn = new int[2]; 
        toReturn[0] = Integer.parseInt(idMatch.get(0)); 
        toReturn[1] = Integer.parseInt(idMatch.get(idMatch.size()-1)); 
        return toReturn; 
       } 
      } 

     } 

     return null; 
    } 

    public static void main(String args[]) 
    { 
     String nodes = "<Node id=\"855\"/>PROFILE<Node id=\"862\"/>:<Node id=\"863\"/>\n" + 
       "<Node id=\"864\"/>8<Node id=\"865\"/> <Node id=\"866\"/>years<Node id=\"871\"/> <Node id=\"872\"/>IT<Node id=\"874\"/> <Node id=\"875\"/>industry<Node id=\"883\"/> <Node id=\"884\"/>experience<Node id=\"894\"/> <Node id=\"895\"/>in<Node id=\"897\"/> <Node id=\"898\"/>web<Node id=\"901\"/> <Node id=\"902\"/>based<Node id=\"907\"/> <Node id=\"908\"/>applications<Node id=\"920\"/> <Node id=\"921\"/>that<Node id=\"925\"/> <Node id=\"926\"/>involved<Node id=\"934\"/> <Node id=\"935\"/>extensive<Node id=\"944\"/> <Node id=\"945\"/>development<Node id=\"956\"/> <Node id=\"957\"/>work<Node id=\"961\"/> <Node id=\"962\"/>in<Node id=\"964\"/> <Node id=\"965\"/>Java<Node id=\"969\"/>/<Node id=\"970\"/>J<Node id=\"971\"/>2<Node id=\"972\"/>EE<Node id=\"974\"/>,<Node id=\"975\"/>Jquery<Node id=\"981\"/>,<Node id=\"982\"/>Jqgrid<Node id=\"988\"/>,<Node id=\"989\"/>Ajax<Node id=\"993\"/>.<Node id=\"994\"/>\n" + 
       "<Node id=\"995\"/>Good<Node id=\"999\"/> <Node id=\"1000\"/>experience<Node id=\"1010\"/>"; 

     String nodes2 = "<Node id=\"855\"/>PROFILE<Node id=\"862\"/>:<Node id=\"863\"/>\n" + 
       "<Node id=\"864\"/>8<Node id=\"865\"/> <Node id=\"866\"/>years<Node id=\"871\"/> <Node id=\"872\"/>IT<Node id=\"874\"/> <Node id=\"875\"/>industry<Node id=\"883\"/> <Node id=\"884\"/>experience<Node id=\"894\"/> <Node id=\"895\"/>in<Node id=\"897\"/> <Node id=\"898\"/>web<Node id=\"901\"/> <Node id=\"902\"/>based<Node id=\"907\"/> <Node id=\"908\"/>applications<Node id=\"920\"/> <Node id=\"921\"/>that<Node id=\"925\"/> <Node id=\"926\"/>involved<Node id=\"934\"/> <Node id=\"935\"/>extensive<Node id=\"944\"/> <Node id=\"945\"/>development<Node id=\"956\"/> <Node id=\"957\"/>work<Node id=\"961\"/> <Node id=\"962\"/>in<Node id=\"964\"/> <Node id=\"965\"/>Java<Node id=\"969\"/>/<Node id=\"970\"/>J<Node id=\"971\"/>2<Node id=\"972\"/>EE<Node id=\"974\"/>,<Node id=\"975\"/>Jquery<Node id=\"981\"/>,<Node id=\"982\"/>Jqgrid<Node id=\"988\"/>,<Node id=\"989\"/>Ajax<Node id=\"993\"/>.<Node id=\"994\"/>"; 
       //"<Node id=\"995\"/>Good<Node id=\"999\"/> <Node id=\"1000\"/>experience<Node id=\"1010\"/>"; 

     String nodes3 = "<Node id=\"1\"/>PROFILE<Node id=\"2\"/>:<Node id=\"3\"/>This<Node id=\"4\"/>is<Node id=\"5\"/>not<Node id=\"6\"/>the<Node id=\"7\"/>Profile<Node id=\"8\"/>\n" + 
       "<Node id=\"855\"/>PROFILE<Node id=\"862\"/>:<Node id=\"863\"/>\n" + 
       "<Node id=\"864\"/>8<Node id=\"865\"/> <Node id=\"866\"/>years<Node id=\"871\"/> <Node id=\"872\"/>IT<Node id=\"874\"/> <Node id=\"875\"/>industry<Node id=\"883\"/> <Node id=\"884\"/>experience<Node id=\"894\"/> <Node id=\"895\"/>in<Node id=\"897\"/> <Node id=\"898\"/>web<Node id=\"901\"/> <Node id=\"902\"/>based<Node id=\"907\"/> <Node id=\"908\"/>applications<Node id=\"920\"/> <Node id=\"921\"/>that<Node id=\"925\"/> <Node id=\"926\"/>involved<Node id=\"934\"/> <Node id=\"935\"/>extensive<Node id=\"944\"/> <Node id=\"945\"/>development<Node id=\"956\"/> <Node id=\"957\"/>work<Node id=\"961\"/> <Node id=\"962\"/>in<Node id=\"964\"/> <Node id=\"965\"/>Java<Node id=\"969\"/>/<Node id=\"970\"/>J<Node id=\"971\"/>2<Node id=\"972\"/>EE<Node id=\"974\"/>,<Node id=\"975\"/>Jquery<Node id=\"981\"/>,<Node id=\"982\"/>Jqgrid<Node id=\"988\"/>,<Node id=\"989\"/>Ajax<Node id=\"993\"/>.<Node id=\"994\"/>" + 
       "PROFILE<Node id=\"1021\"/>:<Node id=\"1022\"/>This<Node id=\"1023\"/>is<Node id=\"1024\"/>not<Node id=\"1025\"/>the<Node id=\"1026\"/>Profile<Node id=\"1027\"/>\n"; 

     String[] el = { "PROFILE", ":", "\n", 
         "8", " ", "years", " ", "IT", " ", "industry", " ", "experience", " ", "in", " ", "web", 
         " ", "based", " ", "applications", " ", "that", " ", "involved", " ", "extensive", " ", 
         "development", " ", "work", " ", "in", " ", "Java", "/", "J", "2", "EE", ",", "Jquery", 
         ",", "Jqgrid", ",", "Ajax", "." 
         }; 

     List<String> elements = Arrays.asList(el); 

     StringMatch sm = new StringMatch(); 
     printTest(sm.findMatches(elements, nodes)); 
     printTest(sm.findMatches(elements, nodes2)); 
     printTest(sm.findMatches(elements, nodes3)); 
    } 

    private static void printTest(int[] vals) 
    { 
     if (vals != null) 
     { 
      System.out.println("found match from id: " + vals[0] + " to " + vals[1]); 
     } 
     else 
     { 
      System.out.println("no match"); 
     } 
     System.out.println("--------------------------------"); 
    } 

} 

有三種測試呼叫的方法和他們返回:

no match 
-------------------------------- 
found match from id: 855 to 993 
-------------------------------- 
found match from id: 855 to 993 
-------------------------------- 
相關問題