Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Trouble

Status
Not open for further replies.

Rieekan

Programmer
Apr 2, 2001
737
US
Trying to write a rule that will take a relative URL passed into the java program and pass it back with a /index.html appended to the end. Seems simple enough, but the part I'm having difficulty with is with the variability of the final slash that may or may not be in the URL that's passed. Any help is appreciated. Below is the code I have so far.

Code:
	rule.setFrom("^/(.*)/[^\\.]+)/?$");
        rule.setTo("$1/index.html");
        
        System.out.println(rule.rewrite("/main/auto/boats/"));

- George
 
As I stated previously, the URL may come in with or without the ending slash due to the information being entered by our end users in the content management system. This is why I went with using a regExp instead of a more simple URL rewrite.

Any help to solve this with a regExp is appreciated.

- George
 
if you really want to stick to the regExp, you could perform it as you did originally. after adding the /.index step back 12 spaces and check for a forward slash. i.e. is there a forward slashes. if it exists remove it because it will be "//.index", else dont.

but stefans works just as easy and involves less of an overhead and all your checking for is a forward slash at the end so why not just use his code?
 
the URL may come in with or without the ending slash

Erm ... thats what stefanwagner's code does :

If the URL endsWith '/', return the URL string plus 'index.html', if not, then return the URL plus a '/' plus the 'index.html'

Perhaps you might see it a bit more simply :

Code:
String url = ...
if (url.endsWith ("/")) {
   url = (url + "index.html");
else {
   url = (url + "/index.html");
}

return url;

There really is no point in trying to be clever and do regex when you don't have to - its more resource hungry, and harder to see what its doing for someone else maintaining your code than the method suggested before !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I will never understand that regex thingie, looks more like assembler than Java.

Anyway, if you want to do it on just a difficult-to-read, professional-looking and optimized-for-nothing single line, I'd bet for
Code:
return new StringBuffer(url.lenght+11).append(url).append((url.endsWith("/"))?"":"/").append("index.html").toString();

Cheers,
Dian
 
Nice Dian :)
Thats got a real "what the heck is that doing" factor !!!

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top