简体   繁体   中英

Matching Strings to a List in Java

So I have this project where I have to match strings (which are really long so I used a util to combine multiple strings into a single string) to passwords (to see how many match) but I have 10,000 passwords I need to check, so I can't manually enter them in one at a time with proper regex expressions. Is there a way I can format them into a list, perhaps with commas? Here is my code currently, with the first few "passwords" manually entered.

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);

//and so on...

but I have SO MANY words/strings i want to match to the original test, (10,000 to be exact) and I have them in a word document and can format them with commas really easily, but putting them in between the formating above would actually take a week. Is there a way to match the original string to a list?

Edit I've gotten it so it reads the code, however it always comes up as the amount of times it runs (11 if I put in 10, 101 if I put in 100, etc.)

int scarlet = 0;
int bible = 0;
int dictionary = 0;
int x = 0;

List<String> passwords = Arrays.asList(password.split(".*\\s,\\s.*"));

 for(x = 0; x <= 10; x++)
 {
if(test.matches(".*?\\b" + passwords + "\\b.*?"))
{
scarlet++;
}
  }

The Above comes out with Scarlet = 11 at the end, if I do (x = 0; x <= 10000; x++) it comes out as 10,001.

try loading your 10000 passwords into a database and then use sql to test if the exist or not. The way you are doing it will (as you say) take forever and is very error prone. Also if you need to maintain your list, you will need to make code changes, recompile, redeploy.

If you read all your passwords into a List, since each match is executing the same code why not just loop them like so

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;
     }

}

I know this might sound too simple, but just checking... lets say you have two lists of strings and you want to check if any string from one list matches a string from another list, have you tried searching by brute force!

//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++;
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM