3

I have a string and from this string, I want to get password file path which is identified by an option (-sn).

String s = "msqlsum81pv 0 0 25 25 25 2  -sn D:\workdir\PV_81\config\sum81pv.pwf -C 5000"

above line is a configuration line which can be with either -sn or -n. please suggest how to get D:\workdir\PV_81\config\sum81pv.pwf line from above string or the string may be with quoted string.

below is my code which check only -sn option but I want to check with either -sn or -n .

if ( s.matches( "^\\s*msql.*$" ) ) 
 {
   StringTokenizer st = new StringTokenizer( s, " " );
   while ( st.hasMoreTokens() )
   {
     if ( st.nextToken().equals( "-sn" ) )
     {
       pwf = st.nextToken();
     }
   }
}

I want to use StreamTokenizer instead of StringTokenizer class and get D:\workdir\PV_81\config\sum81pv.pwf

this path may be containing spaces in it.

String s = "msqlsum81pv 0 0 25 25 25 2  -sn D:\workdir\PV_81\config\sum81pv.pwf -C 5000"



if ( s.matches( "^\\s*msql.*$" ) ) 
 {
   StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(s));
   while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) 
    {

       System.out.println(tokenizer.sval);
    }
}
3
  • 2
    regex is the magic word here. Commented Aug 11, 2016 at 9:32
  • @KevinEsche:- Yes, but I didn't find out a solution. cloud you please suggest some Regex ? Commented Aug 11, 2016 at 9:33
  • Possible duplicate of is there any function in java which behaves like getopt from c Commented Aug 11, 2016 at 9:46

4 Answers 4

4

You should use a regular expression to detect that option in a more general way. If you want a quick fix you can use the OR operator in your if but each time that new operations appear your if will grow and it's a bad idea.

if ( s.matches( "^\\s*msql.*$" ) ) 
 {
   StringTokenizer st = new StringTokenizer( s, " " );
   while ( st.hasMoreTokens() )
   {
     string token = st.nextToken();
     if ( token.equals( "-sn" ) || token.equals("-n" ) )
     {
       pwf = st.nextToken();
     }
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

sidenote: this will fail in case if the path contains a whitespace.
@acostela:- what happened if String containing quoted string "msqlsum81pv 0 0 25 25 25 2 -sn \"D:\workdir\PV_81\config\sum81pv.pwf \" -C 5000"
2

As pointed out on this answer, you could use any good command line arguments parser, like:

More Q&A on command like arguments: this and this.

Comments

2

Use regex, as given in this example

public static void main(String[] args) {
    System.out.println(findString("msqlsum81pv 0 0 25 25 25 2  -sn D:\\workdir\\PV_81\\config\\sum81pv.pwf -C 5000"));
    System.out.println(findString("msqlsum81pv 0 0 25 25 25 2  -s D:\\workdir\\PV_81\\config\\sum81pv.pwf -C 5000"));
    System.out.println(findString("msqlsum81pv 0 0 25 25 25 2  -sn \"D:\\workdir\\PV_81\\config\\sum81pv.pwf\" -C 5000"));
    System.out.println(findString("msqlsum81pv 0 0 25 25 25 2  -s \"D:\\workdir\\PV_81\\config\\sum81pv.pwf\" -C 5000"));

}

private static String findString(String inputCommand) {
    String path;
    if(inputCommand.matches(".*(-sn|-s) \"+.*")) {
        path = inputCommand.replaceAll(".*(-sn|-s) \"?([^\"]*)?.*", "$2");
    } else {
        path = inputCommand.replaceAll(".*(-sn|-s) \"?([^ ]*)?.*", "$2");
    }
    return path;
}

O/P

D:\\workdir\\PV_81\\config\\sum81pv.pwf
D:\\workdir\\PV_81\\config\\sum81pv.pwf
D:\\workdir\\PV_81\\config\\sum81pv.pwf
D:\\workdir\\PV_81\\config\\sum81pv.pwf

Edit: note you might need to modify this if the path could contain whitespace. Then you might want to check until -C or allways escape the whole path and check when the next " will appear.

2 Comments

@YogendraSharma see my edit. i assume that if it is (-s|-sn) \"+ that it also ends with a ". if there is no " i assume that there is no whitespace in the path, as the command wouldn´t be valid otherwise
:- What happened when path containing whitespace. Ex:- "msqlsum81pv 0 0 25 25 25 2 -sn D:\\work dir\\PV 81\\config\\sum81pv.pwf -C 5000"
0

definitely use regular expression,my answer is below

public static String extractString(final String input) {
    String ret = null;
    final Pattern pattern = Pattern.compile("(..\\\\.*\\.*?)(?:\"?)\\p{Space}");
    final Matcher matcher = pattern.matcher(input);
    if (matcher.find()) {
        ret = matcher.group(1);
    }
    return ret;
}

basically, i search from first '\' to first space after dot, and extract this substring, use capture group to filter quote mark if there is one
therefore it doesnt matter where this substring is in this cmd string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.