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)

Community
  • 1
  • 1
  • 1
    This is a very difficult task without knowing possible string formats. Also, paths are usually wrapped with double quotes in such commands. – Wiktor Stribiżew Aug 12 '16 at 07:13
  • you can use : [A-Z]:.*\.\w+ https://regex101.com/r/aE2aR7/5 – Shekhar Khairnar Aug 12 '16 at 07: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. –  Aug 12 '16 at 07: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`](https://regex101.com/r/jI6nX5/6) will be OK. Better - [`-s?n\s*(.*?)\s*-C\s+\d+$`](https://regex101.com/r/jI6nX5/5). – Wiktor Stribiżew Aug 12 '16 at 07:16
  • @WiktorStribiżew: Yes, there is always either -sn or -n before and -C after. or path may be contain whitespace or whiteout whitespace –  Aug 12 '16 at 07:19
  • Then use http://ideone.com/fnJldI – Wiktor Stribiżew Aug 12 '16 at 07:22

4 Answers4

1

You can use:

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

Code Demo

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • :- Thanks, but it will match only `D:\\workdir\\PV` part. please check –  Aug 12 '16 at 07:11
  • You can see [this regex demo](https://regex101.com/r/zG6sK9/4) with all of your inputs. Does it give you your expected results? – anubhava Aug 22 '16 at 11:38
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));
shaifali Gupta
  • 380
  • 1
  • 4
  • 16
0

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

Demo and Explaination

Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
  • Good one, although it should be noted that this doesn't work if there's a dot somewhere _after_ the file name. – SQB Aug 12 '16 at 13:22
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.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 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"`](https://regex101.com/r/tN9mJ8/1). You will have to trim off the leading `"` if present (3rd branch matches it). – Wiktor Stribiżew Aug 22 '16 at 11:05