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);
    }
}
  • 2
    regex is the magic word here. – SomeJavaGuy Aug 11 '16 at 09:32
  • @KevinEsche:- Yes, but I didn't find out a solution. cloud you please suggest some Regex ? –  Aug 11 '16 at 09:33
  • Possible duplicate of [is there any function in java which behaves like getopt from c](http://stackoverflow.com/questions/11783386/is-there-any-function-in-java-which-behaves-like-getopt-from-c) – Tamas Rev Aug 11 '16 at 09:46

4 Answers4

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();
     }
   }
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
acostela
  • 2,597
  • 3
  • 33
  • 50
  • 1
    sidenote: this will fail in case if the path contains a whitespace. – SomeJavaGuy Aug 11 '16 at 09:47
  • @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" –  Aug 11 '16 at 09:49
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.

SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • @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 – SomeJavaGuy Aug 11 '16 at 09:58
  • :- 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" –  Aug 12 '16 at 06:48
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.

Community
  • 1
  • 1
Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
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

dexter
  • 124
  • 6