简体   繁体   中英

How to get specific substring with option vale using java

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

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

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.

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 .

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

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