私は、何が要求されているかについていくつか前提をして、わかりやすく実用的な解決策を作成しました。
私はファイルを読むことを避けています。
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("--------------------------------");
}
}
は、メソッドへの3つのテストの呼び出しがあり、彼らは返す:
no match
--------------------------------
found match from id: 855 to 993
--------------------------------
found match from id: 855 to 993
--------------------------------
私はタイトルに「考慮して、前と次の単語」で混乱しています。また、単語 "プロフィール"の使用。あなたはいくつかの異なる入力を追加して、どんな出力を期待していますか?一致する場合はtrue/falseを返すか、文(要素)が始まるノードファイル内の場所を見つけるか? –
はい、文の開始と終了のノードのIDを探したいと思います。基本的には、開始オフセットと終了オフセットが必要です。 – ganeshk
@hack_on私はそのファイルが何回もプロファイルワードを含むことができることを意味します。しかし、プロファイル単語の残りの文字列は同じでなければなりません。したがって、私たちがプロファイルを3回見つけた場合、最初のプロファイルの次のものはそうではありません:そして、それは何か他のものです。 – ganeshk