10 Replies Latest reply: May 19, 2008 10:12 PM by 807591 RSS

    How Can I extract each part of a string ??

    756081
      Dear Sir:

      I have a string like following to connect to Oracle Database:
      JDBC_URL= jdbc:oracle:thin:@localhost:1521:ABC
      here,
      I have Variables like:
      driverClass="";
      Hostname = "";
      port = "";
      SID="";
      etc

      I try to extract each part from this JDBC_URL,
      then assign them to following variable separately:

      driverClass="jdbc:oracle:thin";
      Hostname = "localhost";
      port = "1521";
      SID="ABC";

      Can some guru help give some good example and show how to do it??

      Thanks and have a nice weekends.
        • 1. Re: How Can I extract each part of a string ??
          807591
          try using a regular expression (regex).
          here, i am going to try to freeball it:
          // JDBC_URL= jdbc:oracle:thin:@localhost:1521:ABC
          
          Pattern p = Pattern.compile("(JDBC_URL)(\\s*)(=)(\\s*)(.*?:.*?:.*?)(:)(@)(.*?)(:)(.*?)(:)(.*?)")
          Matcher m = p.matcher(text)
          
          if(m.matches()) or if(m.lookingAt()){
          
          String driverclass = m.group(5);
          String hostname = m.group(8);
          ...group(10);
          ...group(12);
          
          }
          note that (\\s*) means "any amount of whitespace" and (.*?) means "any amount of any character (reluctantly)".
          i used that because i dont know the constraints on those values. for only numbers you can use (\\d+) etc
          [http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html|http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html]
          also, i put everything into ( ) - which is called a capture group - but that was just to make it clearer. in your final regex you may get
          rid of most of those.
          • 2. Re: How Can I extract each part of a string ??
            756081
            Thanks, Sir:
            I construct following code,
            import java.util.regex.Matcher;
            import java.util.regex.Pattern;
            
            public class JDBCURLexample {
              public static void main(String args[]) {
                String regex = "(\\w+)";
                Pattern pattern = Pattern.compile(regex);
            
                String candidate = "jdbc:oracle:thin:@localhost:1521:ABC";
                String url="";
                String host="";
                String port="";
                String sid="";
            
                Matcher matcher = pattern.matcher(candidate);
            
                int i=0;
                while (matcher.find()) {
                  System.out.println("matcher.group().indexOf("+i+ ") = " + matcher.group());
                  
                  i++;
                }
                System.out.println("Done");
              }
            }
            I am confused how to put each group into: url, host, port, sid variable??
            I tried use group(1), etc, but not success.
            Can you throw more light??
            Thanks
            • 3. Re: How Can I extract each part of a string ??
              807591
              jdbc:oracle:thin:@localhost:1521:ABC


              An alternative is using substrings as follows. (yes, I know its crude and not fool proof):

              String url ="jdbc:oracle:thin:@localhost:1521:ABC";

              String hostName= url.substring(0,url.indexof(":"));

              String databaseName= url.substring(url.indexOf(":")+1;
              databaseName = databaseName.substring(0, databaseName.indexOf(":"));

              ect.
              • 4. Re: How Can I extract each part of a string ??
                800282
                ... and replaceAll(...) can be used:
                String url = "jdbc:oracle:thin:@localhost:1521:ABC";
                System.out.println("driverClass="+url.replaceAll(":@.*", ""));
                System.out.println("hostname="+url.replaceAll("[^@]+@", "").replaceAll(":.*",""));
                System.out.println("port="+url.replaceAll("[^@]+@[^:]+:", "").replaceAll(":.*",""));
                System.out.println("SID="+url.replaceAll(".*:", ""));
                Bur seriously, OP: why not store these variables separately to begin with (or let them come in separately) instead of splitting them afterwards?
                • 5. Re: How Can I extract each part of a string ??
                  756081
                  Thanks so much, It really works,
                  • 6. Re: How Can I extract each part of a string ??
                    800282
                    sunnymanman wrote:
                    Thanks so much, It really works,
                    Yikes, are you going to use that voodoo I posted? Do you understand it?
                    • 7. Re: How Can I extract each part of a string ??
                      756081
                      I just copy and paste then it all works well. It is my project needed, really good.
                      Thanks again, have a good weekend
                      • 8. Re: How Can I extract each part of a string ??
                        756081
                        I roughly understand them, simple but effective
                        • 9. Re: How Can I extract each part of a string ??
                          800282
                          sunnymanman wrote:
                          I roughly understand them, simple but effective
                          Well, okay then. I hope you understand it well enough to adjust it when there's something wrong with it.
                          • 10. Re: How Can I extract each part of a string ??
                            807591
                            Or when you have to alter it because some other url string that is a little different needs to be parsed and the code you were given no longer works.