简体   繁体   中英

Finding substring from a string using regex java

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 )

You can use:

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

Code Demo

RegEx Demo

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

You can also use : [AZ]:.*\\.\\w+

Demo and Explaination

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.

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