[英]Is there any efficient Java solution/Algorithm to find values in a text field based on key string with delimiter?
[英]Is there any Java based open source framework to find values from a text field based on key string with delimiter?
是否有任何基於Java的開放源代碼框架,用於基於帶有定界符的鍵字符串在文本字段中查找值?
例:
密鑰:用戶名Start-Delimiter :; 結束定界符:;
關鍵字:在開始分隔符上: 結束定界符:;
輸入示例:具有用戶名; surenraju;的用戶。 於2013年5月2日星期四下午2點30分登錄系統。
結果:用戶名-surenraju
上-2013年5月2日星期四2:30 pm
提前致謝。
我們已經使用Regex解決了這個問題。
樣例代碼:
ArrayList<String> keys = new ArrayList<String>();
keys.add("username");
keys.add("on");
String startDelimiter = ":";
String endDelimiter = ":";
String searchStr = "A user with username :suren: logged into the system on :22 May 2013 2:30 PM:";
for (String key : keys) {
String pattern = "("+key+")[ ]*?"+startDelimiter+"([^" +endDelimiter+ "]+)"+endDelimiter;
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(searchStr);
while (m.find()) {
System.out.println("Key: " + m.group(1) + " Value: "
+ m.group(2));
}
}
測驗
一,密鑰:用戶名Start-Delimiter: 結束定界符:;
需要找到一個值,如果該值后跟鍵(在本例中為username),並且該值在開始和結束定界符(在本例中為;)之間。
二。 關鍵字:在開始分隔符上: 結束定界符:;
輸入樣例:
具有用戶名; suren;的用戶 於2013年5月2日星期四下午2點30分登錄系統。
結果:I.用戶名-確保II。 上-2013年5月2日星期四2:30 pm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.