Regular expressions jsp

Posted on 2007-07-25
Last Modified: 2008-02-01
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.
Question by:olenadmytrenko
    LVL 92

    Expert Comment

    why don't you just decode the string?
    LVL 92

    Expert Comment

    if u want to use regexp try something like:

    s = s.replaceAll("%3A", ":").replaceAll("%2F", "/");
    LVL 26

    Expert Comment

    I am not sure if I understand your problem clearly...

    String str = "http%3A%2F%2F"
    str = str.replaceAll( str, "http://" )
    LVL 26

    Expert Comment

    yes, as objects said, decoding the string would resolve the problem and that is the standard way too!

    Author Comment

    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....
    LVL 63

    Accepted Solution

    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://" />


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    wordsFront challenge 8 49
    mergeTwo  challenge 13 55
    count11 challenge 6 35
    XML Paring  Error - Premature end of file. 7 37
    by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    The viewer will learn how to implement Singleton Design Pattern in Java.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now