0

I have a String:

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

I want to get the path (in this case D:\\workdir\\PV 81\\config\\sum81pv.pwf) from this string. This path is an argument of a command option -sn or -n, so this path always appears after these options.

The path may or may not contain whitespaces, which needs to be handled.

public class TestClass {

     public static void main(String[] args) {
         String path;
         String s = "msqlsum81pv 0 0 25 25 25 2  -sn D:\\workdir\\PV 81\\config\\sum81pv.pwf -C 5000";
         path = s.replaceAll(".*(-sn|-n) \"?([^ ]*)?", "$2");
         System.out.println("Path: " + path);
     }
 }

Current output: Path: D:\workdir\PV 81\config\sum81pv.pwf -C 5000
Expected output: Path: D:\workdir\PV 81\config\sum81pv.pwf

Below Answers working fine for the earlier case.

i need a regex which return `*.pwf` path if the option is `-sn, -n, -s, -s -n, or without -s or -n.`

But if I have below case then what would be the regex to find password file.

String s1 = msqllab91 0 0 1 50 50 60 /mti/root/bin/msqlora    -n "tmp/my.pwf" -s 
String s2 = msqllab92 0 0 1 50 50 60 /mti/root/bin/msqlora -s -n /mti/root/my.pwf
String s3 = msqllab93 0 0 1 50 50 60 msqlora        -s -n "/mti/root/my.pwf" -C 10000 
String s4 = msqllab94 0 0 1 50 50 60 msqlora.exe    -sn   /mti/root/my.pwf 
String s5 = msqllab95 0 0 1 50 50 60 msqlora.exe    -sn   "/mti/root"/my.pwf 
String s6 = msqllab96 0 0 1 50 50 60 msqlora.exe    -sn"/mti/root"/my.pwf 
String s7 = msqllab97 0 0 1 50 50 60 "/mti/root/bin/msqlora" -s -n /mti/root/my.pwf -s
String s8 = msqllab98 0 0 1 50 50 60 /mti/root/bin/msqlora -s
String s9 = msqllab99 0 0 1 50 50 60 /mti/root/bin/msqlora -s -n /mti/root/my.NOTpwf -s -n /mti/root/my.pwf
String s10 = msqllab90 0 0 1 50 50 60 /mti/root/bin/msqlora -sn /mti/root/my.NOTpwf -sn /mti/root/my.pwf
String s11 = msqllab901 0 0 1 50 50 60 /mti/root/bin/msqlora
String s12 = msqllab902 0 0 1 50 50 60 /mti/root/msqlora-n NOTmy.pwf
String s13 = msqllab903 0 0 1 50 50 60 /mti/root/msqlora-n.exe NOTmy.pwf

i need a regex which return *.pwf path if the option is -sn, -n, -s, -s -n, or without -s or -n.

path contains *.pwf file extension only not NOTpwf or any other extension and code should all work except the last two because it is an invalid command.

Note: I already asked this type of question but didn't get anything working as per my requirement. (How to get specific substring with option vale using java)

6
  • 1
    This is a very difficult task without knowing possible string formats. Also, paths are usually wrapped with double quotes in such commands. Commented Aug 12, 2016 at 7:13
  • you can use : [A-Z]:.*\.\w+ regex101.com/r/aE2aR7/5 Commented Aug 12, 2016 at 7:15
  • @WiktorStribiżew:- Thanks, The String formats are the same as above. i just need to get .pdf file path from this configuration string and this password file is an argument to command option either -sn or -n. Commented Aug 12, 2016 at 7:15
  • 1
    @TimBiegeleisen: Why, you can always suggest something. I just know that matching paths with spaces in them is hard without knowing the context. If OP confirms there is always -sn before and -C after, -s?n\s*(.*?)\s*-C\b will be OK. Better - -s?n\s*(.*?)\s*-C\s+\d+$. Commented Aug 12, 2016 at 7:16
  • @WiktorStribiżew: Yes, there is always either -sn or -n before and -C after. or path may be contain whitespace or whiteout whitespace Commented Aug 12, 2016 at 7:19

4 Answers 4

1

You can use:

path = s.replaceFirst(".*\\s-s?n\\s*(.+?)(?:\\s-.*|$)", "$1");
//=> D:\workdir\PV 81\config\sum81pv.pwf

Code Demo

RegEx Demo

Sign up to request clarification or add additional context in comments.

2 Comments

:- Thanks, but it will match only D:\\workdir\\PV part. please check
You can see this regex demo with all of your inputs. Does it give you your expected results?
0

Try this

String s = "msqlsum81pv 0 0 25 25 25 2  -sn D:\\workdir\\PV 81\\config\\sum81pv.pwf -C 5000";
    int l=s.indexOf("-sn");
    int l1=s.indexOf("-C");
    System.out.println(s.substring(l+4,l1-2));

Comments

0

You can also use : [A-Z]:.*\.\w+

Demo and Explaination

1 Comment

Good one, although it should be noted that this doesn't work if there's a dot somewhere after the file name.
0

Rather than using complex regexps for replacing, I'd rather suggest a simpler one for matching:

String s = "msqlsum81pv 0 0 25 25 25 2  -sn D:\\workdir\\PV 81\\config\\sum81pv.pwf -C 5000";
Pattern pattern = Pattern.compile("\\s-s?n\\s*(.*?)\\s*-C\\s+\\d+$");
Matcher matcher = pattern.matcher(s);
if (matcher.find()){
    System.out.println(matcher.group(1)); 
} 
// => D:\workdir\PV 81\config\sum81pv.pwf 

See the IDEONE Demo

If the -C <NUMBER> is optional at the end, wrap with an optional group -> (?:\\s*-C\\s+\\d+)?$.

Pattern details:

  • \\s - a whitespace
  • -s?n - a -sn or -n (as s? matches an optional s)
  • \\s* - 0+ whitespaces
  • (.*?) - Group 1 matching any 0+ chars other than a newline
  • \\s* - ibid
  • -C - a literal -C
  • \\s+ - 1+ whitespaces
  • \\d+ - 1 or more digits
  • $ - end of string.

1 Comment

I do not think I understand the new specs. The best I can suggest is "(?<!\\d\\s)(?<!\\d\\s")(?:\\B/(?:[^/]+/)+[^/]+|\\b[A-Z]:(?:\\[^\\]+)+|\"\\S+)\\.pwf". You will have to trim off the leading " if present (3rd branch matches it).

Your Answer

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