Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1544
  • Last Modified:

Regular expressions jsp

I am trying to replace encoded http%3A%2F%2F with http:// in a string using regular expressions in a jsp...
Not much luck so far, it replaces the first occurrence but the second one is replaced OK, but ":" column is missing...
So the second occurrence became http//.
I actually don't need to replace the second occurrence, but only the first one.
My string always starts with http%3A%2F%2F.
What is the correct regex and replacement for this using regular expressions?
 Basically i need
http%3A%2F%2F become http:// only ones.
Thank you.
0
olenadmytrenko
Asked:
olenadmytrenko
1 Solution
 
objectsCommented:
why don't you just decode the string?
0
 
objectsCommented:
if u want to use regexp try something like:

s = s.replaceAll("%3A", ":").replaceAll("%2F", "/");
0
 
ksivananthCommented:
I am not sure if I understand your problem clearly...

String str = "http%3A%2F%2F"
str = str.replaceAll( str, "http://" )
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ksivananthCommented:
yes, as objects said, decoding the string would resolve the problem and that is the standard way too!
0
 
olenadmytrenkoAuthor Commented:
The thing is that string must be encoded for some rating purposes.
I need to decode  http:// because if the string starts with encoded value
which is used in an ancor (a href=""), then the whole url is treated as  relative..
So if http:// is http%3A%2F%2F, then there is http://localhost:8080 is appended infront of my  encoded http://

Basically  I have a custom tag which can replace substrings based on regular expressions, like
<cas:replace regex="http%3A%2F%2F" replacement="http://"/>
so I thought I'll use it, it does everything ok, except I am not sure about the string in replacement part, coz it accpets regular expressions and I am rubbish with them.
If it all sounds too complicated then I could use just java string relpace....
thanks
0
 
ZvonkoSystems architectCommented:
The normal behaviour is that repalce methods do replace oncce when NOT searching by RegExp.
So look for your <cas:replace what the contepart for regex= keyword is and you are done. Then you need something like this:

<cas:replace  search="http%3A%2F%2F" replacement="http://"/>

I did not found cas tag library description, but I assume it is something like:

<cas:replace  if="http%3A%2F%2F" replacement="http://" />

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now