3

I've read on here how to split data from a string and it worked wonders... for the most part.

What I am trying to do is pull data from a credentials file that has four sections on a line seperated by spaces, the username, MD5 hash, plain text password surrounded in ", and finally a role. My issue is that I began using the .split thinking that that would be what I needed, but that third or [2] item of occasionally has a space in it that I need to somehow filter into the split. Any ideas?

An example of the line that gets split from credentialLine:

griffin.keyes 108de81c31bf9c622f76876b74e9285f "alphabet soup" zookeeper

    FileInputStream credentialsFile = null;
    Scanner inFS = null;
    String credFile = ".\\src\\authenticationsystem\\credentials.txt";

    System.out.println("\nOpening file credentials.txt");

    credentialsFile = new FileInputStream(credFile);
    inFS = new Scanner(credentialsFile);
    String credentialLine = inFS.nextLine();
    System.out.println(credentialLine);
    System.out.println("Closing file credentials.txt\n");
    credentialsFile.close();

    String[] userCreds = credentialLine.split("\\s+");
    String userCred = userCreds[0];
    String userMD = userCreds[1];
    String userPass = userCreds[2];
    String userRole = userCreds[3];
Ghostey
  • 33
  • 3

2 Answers2

2

You'll need to use a regex that ignores some spaces. From the example you've provided it seems that the space you want to ignore is the one inside the quotes.

If that is the case for all the values you can use something like this:

String[] userCreds = credentialLine.split("((?!\"[^\" ]+)\\s+(?![^ \"]+\"))");

((?!\"[^\" ]+) means: make sure there isn't a quote fallowed by a sequence of characters (other then space or quote) before/after one or multiple spaces.

Titus
  • 22,031
  • 1
  • 23
  • 33
  • Don't be so quick on this one. It fails if there is **no space** in the plain text password. After all, it's only occasionally that there is a space there. – DevilsHnd - 退職した Feb 19 '17 at 06:42
  • I received the below Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at authenticationsystem.AuthenticationSystem.main(AuthenticationSystem.java:48) C:\Users\Anna\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 2 seconds) – Ghostey Feb 19 '17 at 06:46
  • @DevilsHnd You're right, I haven't considered that. – Titus Feb 19 '17 at 06:48
  • Devils, good to know considering there are two lines with passwords that do not have spaces between the quotes. – Ghostey Feb 19 '17 at 06:48
  • Is it possible the split is not the best approach to have taken for this? – Ghostey Feb 19 '17 at 06:51
  • @MichaelWhite I've edited the regex to account for those cases as well. – Titus Feb 19 '17 at 06:52
  • Thanks @Titus, I still get an ArrayIndexOutofBoundsException. Also where can I learn about all the craziness you just offered me. lol. This is for a final project and its the only piece I'm stuck on. – Ghostey Feb 19 '17 at 06:55
  • @Ghostey Can you show me the `String` that is causing this exception ? This is called Regular Expression (regex) and this is a good place to get started learning it http://stackoverflow.com/a/2759417/1552587 – Titus Feb 19 '17 at 06:59
  • @Titus Do you mean what it is trying to pull? that griffin.keys line in my original post is a sample of the data. They all look very similar with the exception of the occasional plain text password being "redfield" instead of "red field" But the string that I edited for this to stat happening is now. String[] userCreds = credentialLine.split("((?!\"[^\" ]+)\\s+(?![^ \"]+\"))"); – Ghostey Feb 19 '17 at 07:04
  • @Ghostey I'm not sure why that is, I've just tested this and it seems to work as expected. Here is an example https://ideone.com/xwrDKb – Titus Feb 19 '17 at 07:14
  • @Titus Using Netbeans, btw Line 50 & 51 String userPass = userCreds[2]; String userRole = userCreds[3]; – Ghostey Feb 19 '17 at 07:23
  • @Ghostey The error is caused because there is a line in the file that isn't split in as many parts as the others. I will need to know the value of `credentialLine` before this exception occurs. – Titus Feb 19 '17 at 07:28
  • @Titus I don't think I follow. credentialLine is declared in the above statement, its first and initial value is when it pulls the griffin.keyes line from a text document. Titus I want you to know that I really appreciate your help with this. Is there an easier way for us to chat? – Ghostey Feb 19 '17 at 07:37
  • I removed the userRole element and it went through successful I noticed that the MD5 hash and the username are being pulled into userCred, which should only be the username. – Ghostey Feb 19 '17 at 07:38
  • @Ghostey That will happen if the "MD5 hash" contains quotes. – Titus Feb 19 '17 at 07:45
  • @Titus It doesn't though, only numbers and letters. – Ghostey Feb 19 '17 at 07:46
  • @Titus I did some testing that showed that it was happening to all lines in the credential file. So I took the cred[0] and sent it back into the scanner and used .next() to separate it. – Ghostey Feb 19 '17 at 08:31
0
String stringToSearch = "griffin.keyes 108de81c31bf9c622f76876b74e9285f \"alphabet soup\" zookeeper";

Pattern p1 = Pattern.compile("(?:[^\" ]+|\"[^\"]+\")+");
Matcher m = p1.matcher(stringToSearch);
while (m.find())
{   
    System.out.println(m.group());
}

When it encounters qutoes, the group alternates to the right hand side, which matches everything between the quotes, and returns to the left hand side for matching everything up to the next space.
Avaneesh
  • 162
  • 5