2014-01-22 25 views
0

所以我有這個項目,我必須匹配字符串(這真的很長,所以我用util將多個字符串組合成一個字符串)到密碼(查看多少匹配),但我有10,000個密碼需要檢查,所以我不能用適當的正則表達式一次一個地手動輸入它們。有沒有辦法可以用逗號將它們格式化爲列表?這是我目前的代碼,手動輸入了前幾個「密碼」。將字符串匹配到Java中的列表

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


public class RegexTestPatternMatcher { 
public static final String test = org.apache.commons.lang3.StringUtils.join(new String[] { 
"multiple", 
"strings", 
"here" 
}); 

public static final String bib = org.apache.commons.lang3.StringUtils.join(new String[] { 

"different", 
"strings", 
"here" 
}); 

public static final String dict = org.apache.commons.lang3.StringUtils.join(new String[] { 
"even more", 
"strings", 
"here" 
}); 

List<String> testlist = Arrays.asList(dict.split("\\s*.,\\s*.")); 
public static void main(String[] args) { 
Pattern pattern = Pattern.compile("\\S+", Pattern.CASE_INSENSITIVE); 
// in case you would like to ignore case sensitivity, 
// you could use this statement: 
// Pattern pattern = Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE); 
Matcher matcher = pattern.matcher(test); 
// check all occurance 
while (matcher.find()) 
{ 
    System.out.print("Start index: " + matcher.start()); 
    System.out.print(" End index: " + matcher.end() + " "); 
    System.out.println(matcher.group()); 
} 

// now create a new pattern and matcher to replace whitespace with tabs 
Pattern replace = Pattern.compile("\\s+"); 
Matcher matcher2 = replace.matcher(test); 
System.out.println(matcher2.replaceAll("\t")); 
double scarlet = 0; 
double bible = 0; 
double dictionary = 0; 

if(test.matches(".*?//bpassword//b.*?")) 
{ 
scarlet += 1; 
System.out.println("hello?"); 
} 
    if(test.matches(".*?\\b123456\\b.*?")) 
{ 
scarlet += 1; 
} 
    if(test.matches(".*?\\b12345678\\b.*?")) 
{ 
scarlet += 1; 
} 
    if(test.matches(".*?\\b1234\\b.*?")) 
{ 
scarlet += 1; 
} 
    if(test.matches(".*?\\bqwerty\\b.*?")) 
{ 
scarlet += 1; 
} 
    if(test.matches(".*?\\b12345\\b.*?")) 
{ 
scarlet += 1; 
} 
    if(test.matches(".*?\\bdragon\\b.*?")) 
{ 
scarlet += 1; 
} 
    if(test.matches(".*?\\bpussy\\b.*?")) 
{ 
scarlet += 1; 
} 
System.out.println("Scarlet Letter Matches: " + scarlet); 

//等等...

,但我有這麼多的話/串我想匹配到原來的測試,(10,000要準確),我讓他們在Word文檔中並且可以很容易地用逗號格式化它們,但是將它們放在上面的格式之間實際上需要一週的時間。有沒有辦法將原始字符串與列表進行匹配?

編輯 ,所以它讀取的代碼中,我得到它,但它總是出來作爲它的運行倍量(11,如果我把在10,101,如果我把100等)

​​

如果我這樣做(x = 0; x < = 10000; x ++),上面出現Scarlet = 11,結果爲10,001。

+0

請清理並格式化您的代碼。只在你的問題上發佈有意義的代碼。 – everton

+0

您有一個文件中的密碼列表,並且您有一個字符串列表,您需要檢查是否與這些密碼匹配。我理解這個權利嗎? –

+0

@EvertonAgner對不起,我對Java和編程非常陌生,不知道什麼對解決問題有幫助,什麼不是,只是想給他們儘可能多的信息。 – user3221458

回答

0

嘗試將您的10000個密碼加載到數據庫中,然後使用sql來測試是否存在或不存在。 你這樣做的方式將會(如你所說)永遠持續下去,並且非常容易出錯。此外,如果您需要維護您的列表,則需要更改代碼,重新編譯並重新部署。

0

如果你讀了所有的密碼到一個列表,因爲每場比賽正在執行相同的代碼,爲什麼不只是他們圈像這樣

List<String> passwords = new ArrayList<String>(); 
// read in your passwords... from a file? 
for(String s : passwords) 
{ 
    if(test.matches(".*?\\b" + s + "\\b.*?")) 
    { 
      scarlet += 1; 
    } 

} 
+0

我剛剛編輯了原文,我用了很多你發佈的內容,它幫助了很多!但是,一旦我得到它讀取,它總是出來爲0,或者如果我做一個循環,如(x = 0; x <= 10; x ++)它出來爲11(例如) – user3221458

+0

通過打印出您匹配的模式和字符串來檢查您的正則表達式,並在正則表達式站點(如[RegexPlanet](http://www.regexplanet.com/))上進行測試。另外,如果你使用for循環,你需要使用'passwords.get(x)' –

0

我知道這聽起來太簡單了,但只是檢查...可以說你有兩個字符串列表,你想檢查一個列表中的任何字符串是否與另一個列表中的一個字符串匹配,你是否嘗試過使用蠻力搜索!

//List<String> testwords = your list of test words 
//List<String> passwords = your list of passwords 

    for(String test: testwords) { 
     for(String password: passwords) { 
      if(test!=null && test.equals(password)) { 
       scarlet++; 
      } 
     } 
    }