0

I have a string in the following format, I only need to extract the /jspFolderTestSecondLast/jspFolderTestLast, which is the second last seperated by /.

www.name.com/jspFolderTestOne/jspFolderTestTwo/jspFolderTestAndmanyMore/jspFolderTestSecondLast/jspFolderTestLast

/jspFolderTestSecondLast/jspFolderTestLast can be varied in length but always gonna be separated by secong last /.

Any help is appreciated.

Thanks

1
  • As usual: what have you tried? Commented Jan 12, 2011 at 17:03

5 Answers 5

2
String s = "www.name.com/jspFolderTestOne/jspFolderTestTwo/jspFolderTestAndmanyMore/jspFolderTestSecondLast/jspFolderTestLast"
String[] parts = s.split("/");
String whatYouWant = parts[parts.length-2] +"/" + parts[parts.length-1]
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need any regexes for that, since you can just split the string on '/' and get two last array indexes. But here's the regex anyway:

^.+(/[^/]+)(/[^/]+)$

$1 contains the first and $2 contains the second block

Comments

1
String str = "www.name.com/jspFolderTestOne/jspFolderTestTwo/jspFolderTestAndmanyMore/jspFolderTestSecondLast/jspFolderTestLast";

String are[]  = str.split("/");//may be you need to add escape here
//take last two parts

Comments

1
    Pattern p = Pattern.compile(".*(/[^/]+/[^/]+)$");
    Matcher m = p.matcher("a/b/c/d.txt");
    if( m.matches() ) {
        System.out.println(m.group(1));
    }

Comments

1

this is the javascript version:

"www.name.com/jspFolderTestOne/jspFolderTestTwo/jspFolderTestAndmanyMore/jspFolderTestSecondLast/jspFolderTestLast".search(/\/[^\/]*\/[^\/]*$/)

or you can group them nicely:

(/(\/[^\/]*)(\/[^\/]*)$/)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.