Using Java's Regex to extract a word from a path name -


i have directory , trying extract word "photon" before "photon.exe".

c:\workspace\photon\output\i686\diagnostic\photon.exe(suspended) thread(running)

my code looks this:

string path = "c:\workspace\photon\output\i686\diagnostic\photon.exe(suspended) thread(running)"; pattern pattern = pattern.compile(".+\\\\(.+).exe");  matcher matcher = pattern.matcher(path);  system.out.println(matcher.group(1)); 

no matter permutations try keep getting illegalstateexceptions etc, despite regular expression working on http://www.regexplanet.com/simple/index.html.

thanks in advance help. super frustrated @ point >.<

you can use following regular expression: ^.*\\(.*)\.exe.*$ , file name in first match group. here example.

import java.util.regex.matcher; import java.util.regex.pattern;  public class main {     public static void main(final string[] args)     {         final string input = args[0];         final pattern pattern = pattern.compile("^.*\\\\(.*)\\.exe.*$");         final matcher matcher = pattern.matcher(input);         if (matcher.find())         {             system.out.println("matcher.group(1) = " + matcher.group(1));         }         else         {             system.out.format("%s not match %s\n", input, pattern.pattern());         }     } } 

run c:\workspace\photon\output\i686\diagnostic\photon.exe(suspended) thread(running) input , here expected output:

matcher.group(1) = photon 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -