所以我有這個項目,我必須匹配字符串(這真的很長,所以我用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。
請清理並格式化您的代碼。只在你的問題上發佈有意義的代碼。 – everton
您有一個文件中的密碼列表,並且您有一個字符串列表,您需要檢查是否與這些密碼匹配。我理解這個權利嗎? –
@EvertonAgner對不起,我對Java和編程非常陌生,不知道什麼對解決問題有幫助,什麼不是,只是想給他們儘可能多的信息。 – user3221458