Java split regex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FreshRob
    New Member
    • Jul 2009
    • 12

    Java split regex

    Hello,

    I want to split a string starting from ${ and ending with }
    where anythign can be inbetween ( ${*} )

    I have tried
    Code:
    String url2 = "http://someurl${language}somemore.com";
     String[] url3 = url2.split("\\$\\{\\w\\}");
    However I am getting no luck with it,
    help would be greatly appreciated
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    FreshRob,

    I think you'd be better off walking the string and extracting each segment in turn using a recognizer pattern, but it all depends on the language you're trying to implement.

    If you just want to do the split, try something like:
    Code:
    String url3[] = url2.split("(\\$\\{)|(\\})");
    The problem with what you're doing, is that it will split strings like this:
    Code:
    String urlBad = "http://${insertOne${insertTwo}more}";
    into:
    Code:
    String urlBadBits[] = {"http://", "insertOne", "insertTwo", "more"};
    Which is probably a bad thing.

    Comment

    • FreshRob
      New Member
      • Jul 2009
      • 12

      #3
      Hello Oralloy,

      Thank you for your reply.
      Lookign at it from your perspective I can see where I am making the mistake

      I believe what I really require is to split the string before ${ and then split it after }
      where
      url3[0] = http://someurl
      url3[1] = ${language}
      url3[2] = somemore.com

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Right,

        So if you were to use the pattern class to break up the string into the three pieces, what sort of pattern would you consider using?

        What I'm guiding you towards is a solution where you iteratively process the string and work off all your substitutions. Am I making sense?

        Comment

        • FreshRob
          New Member
          • Jul 2009
          • 12

          #5
          I should probably give some background to my problem.

          The users pass a string with a url
          e.g. "http://www.somedomain. com
          within the url they set some parameters ${language}

          The code then searches the code finds these strings and replaces them with the correct variable.

          Because its going to be dynamic, I require the code to see if the parameter exists and if so replaces it.

          I believe he premise of what I was trying to do is illogical and not feasible.

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            FreshRob,

            The problem is at what point do you stop doing this replacement strategy. The code you want is actually fairly straightforward to implement, just tedious.

            Also, error handling is going to be a pain, as well.

            Basically all you need to do is repeatedly recognize against the head of the string, and strip off the part that you've just done, adding it to an output string buffer.

            What happens is that users put in strange forms, like the one I showed in the error case of my first post.

            So....some basic pseudo-Java code, because I don't have time to write and debug this:
            Code:
            String interpolate(String input, Map<String, String>vars)
            {
              String in = input;
              StringBuilder out = new StringBuilder();
            
              String patVar = "(\\$\\{\\w+\\})";
              String patStr = "([^$]+)";
              Pattern p = Pattern.compile("^(" + patStr + "|" +patVar + ")");
            
              int limit = input.length();
              while(not in.equals(""))
              {
                Matcher m = p.matcher(in);
                if (not m.matches())
                  throw new Error("Bad input string fella.");
            
                String bit = m.group();     // grab the matched bit
            
                int bitLen = bit.length();  // chop off the input bits
                in = in.substring(bitLen);
            
                if (bit.charAt(0) != '$')
                  out.append(bit);
                else if (vars.comtainsKey(bit.substring(2,bitLen-1))
                  out.append(vars(bit.substring(2,bitLen-1));
                else
                  throw new Error("what is this useless bit: \"" + bit + "\"?");
              }
            }
            Last edited by Oralloy; Sep 30 '10, 02:49 PM. Reason: fixed code tags

            Comment

            • FreshRob
              New Member
              • Jul 2009
              • 12

              #7
              Thank you very much this was a lot of help

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                FreshRob,

                Glad to help.

                Cheers!

                Comment

                Working...