6 Replies Latest reply: Apr 12, 2007 8:06 AM by 800282 RSS

    Replace All with Regular Expression

    807606
      Hi all,

      I need a help to replace the String:
      "[
      ]"
      to
      "<a href=\"1\"">Java Programming</a>"
      
      I was trying to replace with 
      
      .replaceAll("\\[\\[*([^\\]]*?)*\\]\\]", "<a href=\"\"></a>")
      
      but I don't know how to separate the parameters values.
      
      Best regards                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        • 1. Re: Replace All with Regular Expression
          807606
          i like your nick ;)
          • 2. Re: Replace All with Regular Expression
            800282
            How about something like this:
            import java.util.regex.Matcher;
            import java.util.regex.Pattern;
            
            class Main { 
                
                public static void main(String[] args) {
                    String line = "[[Code: 1 Title: Java Programming]]";
                    String newLine = "<a href=\""+
                                    get("Code:", " ", line)+
                                    "\">"+
                                    get("Title:", "]", line)+
                                    "</a>";
                    System.out.println(line);
                    System.out.println(newLine);
                }  
                
                public static String get(String start, String end, String text) {
                    Pattern pattern = Pattern.compile("(?<="+start+"\\s)(.*?)(?="+end+")");
                    Matcher matcher = pattern.matcher(text);
                    return matcher.find() ? matcher.group() : "#ERROR#";
                }
            }
            • 3. Re: Replace All with Regular Expression
              807606
              Hi prometheuzz,

              you are the one!!

              But how I could do to a Srting like this:
                      String line = "any text any text any text any text any text.\n"+
                                             "Click - [[Code: 6 Title: Java Programming]].\n"+
                                             "any text any text any text any text any text .\n"+
                                             "- [[C�digo: 2 T�tulo: Sun]] text text\n"+ 
                                             "any text any text any text any text any text.";
              Thanks a lot for you help
              • 4. Re: Replace All with Regular Expression
                800282
                Hi prometheuzz,

                you are the one!!

                But how I could do to a Srting like this:
                Ah, more requirements...
                Things are getting a bit messy now:
                import java.util.regex.Matcher;
                import java.util.regex.Pattern;
                
                class Main {  
                    
                    public static void main(String[] args) {
                        String line = "any text any text any text any text any text. "+
                                      "Click - [[Code: 6 Title: Java Programming]]. "+
                                      "any text any text any text any text any text. "+
                                      "- [[C�digo: 2 T�tulo: Sun]] text text "+ 
                                      "any text any text any text any text any text."; 
                        String newLine = line;
                        String[] wikiTags = getWikiTags(newLine);
                        for(String tag : wikiTags) {
                            String newTag = "<a href=\""+get("(Code:|C�digo:)", " ", tag)+
                                            "\">"+get("(Title:|T�tulo:)", "$", tag)+"</a>";
                            newLine = newLine.replaceFirst("\\[\\["+tag+"\\]\\]", newTag);
                        }
                        System.out.println(line);
                        System.out.println(newLine);
                    }  
                    
                    public static String[] getWikiTags(String text) {
                        java.util.List<String> list = new java.util.ArrayList<String>();
                        Pattern pattern = Pattern.compile("(?<=\\[\\[)(.*?)(?=\\]\\])");
                        Matcher matcher = pattern.matcher(text);
                        while(matcher.find()) {
                            list.add(matcher.group());
                        }
                        return list.toArray(new String[list.size()]);
                    }
                
                    public static String get(String start, String end, String text) {
                        Pattern pattern = Pattern.compile("(?<="+start+"\\s)(.*?)(?="+end+")");
                        Matcher matcher = pattern.matcher(text);
                        return matcher.find() ? matcher.group() : "#ERROR#";
                    }
                }
                Details about regex:
                http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
                http://www.regular-expressions.info/java.html
                http://java.sun.com/docs/books/tutorial/essential/regex/


                >
                Thanks a lot for you help
                Like I said, it's a bit of a messy solution. See if you can use (a part of) it.
                Good luck.
                • 5. Re: Replace All with Regular Expression
                  807606
                  Thanks a lot for your help prometheuzz!!
                  Certainly I will use your code and read those docs that you sent to improve my knowledge.

                  Best regards

                  Canabz
                  • 6. Re: Replace All with Regular Expression
                    800282
                    Thanks a lot for your help prometheuzz!!
                    Certainly I will use your code and read those docs
                    that you sent to improve my knowledge.

                    Best regards

                    Canabz
                    No problem, and good luck!