Results 1 to 6 of 6

Thread: Regex - replace special characters

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Regex - replace special characters

    Hello,
    My application crashes when I try to replace these characters "* \?" Yet I have to filter them under windows because it is impossible to create a directory with these characters. I am trying to replace special characters. But this is not working for me.
    Here is my code
    Code:
    if(! query.getText().equals("")){
                  / / Create the tree
                  pt = zept.getText();//+"/"+ requete.getText () / / tree management
                  File fb = new File(pt);
                  fb.mkdir();
                  rqnm = query.getText();
                  rqnm = rqnm.replaceAll(":","_");
                / / rqnm = rqnm.replaceAll ( "\ \ ","");
                  rqnm = rqnm.replaceAll("<"," ");
                  rqnm = rqnm.replaceAll(">"," ");
                  rqnm = rqnm.replaceAll("|"," ");
                / / rqnm = rqnm.replaceAll ("?"," ");
                  rqnm = rqnm.replaceAll("/"," ");
                / / rqnm = rqnm.replaceAll ("*"," ");
              	 
                  pt = zept.getText()+"/"+ rqnm; / / tree management
                  File fl = new File(pt);
                  fl.mkdir();
    Please check the code and if you have any idea about it then please do post back.

  2. #2
    Join Date
    Nov 2009
    Posts
    446

    Re: Regex - replace special characters

    Hello,
    Some of these characters are interpreted by specific regexp. For example * means that the preceding element can be present 0 or more times. So if you used the wrong regular expression is incorrect and will cause an exception.
    You have to protect them with a double \:
    Code:
    rqnm. = rqnm..replaceAll ( "\\*"," ");

  3. #3
    Join Date
    Nov 2009
    Posts
    330

    Re: Regex - replace special characters

    Hello,
    If you want to try for the backslash, then you can try the following.
    Code:
    rqnm. = rqnm..replaceAll("<a href ="http:/ / www.example.net/forums/ "target =" _blank "> \ \ \ \ </ a >","");
    So for the characters? and * you have to be preceded by \ \ because they are also special characters. Hope this information will help you, if not then please post back with your query and we will try to solve the problem.

  4. #4
    Join Date
    Nov 2009
    Posts
    335

    Re: Regex - replace special characters

    Hello,
    A small peek into the javadoc have said that the method replaceAll() Class String expects a regular expression to its first parameter. The characters '*', '?', '\' Are reserved characters for regular expressions you must escape hence.
    Code:
    rqnm. = rqnm..replaceAll("\\\\"," ");
    rqnm. = rqnm..replaceAll("\\*"," ");
    rqnm. = rqnm..replaceAll("\\?"," ");
    Moreover, it is the same for the character '|'
    Code:
    Code:
    rqnm. = rqnm..replaceAll("\\|"," ");

  5. #5
    Join Date
    Dec 2009
    Posts
    213

    Re: Regex - replace special characters

    Hello,
    This is my updated code, please check if it is correct, and if you find something wrong in the code the please correct me.
    Code:
    if(! query.getText().equals("")){
                  / / Create the tree
                  pt = zept.getText(); / / tree management
                  File fl = new File(pt);
                  fl.mkdir();
                  if(System.getProperty("os.name").startsWith("Windows"))  
                  {
                   rqnm. = query.getText();
                   rqnm. = rqnm..replaceAll(":|\\\\|/", "_").replaceAll("<|>|\\||\\?|/|\\*"," ");
    
                   pt = zept.getText()+"/"+ rqnm.; / / tree management
                  File fl2 = new File(pt);
                  fl2.mkdir();
                  }
                  else {
                	  pt = zept.getText()+"/"+ query.getText(); / / tree management
                      File fl2 = new File(pt);
                      fl2.mkdir();
                  }

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: Regex - replace special characters

    Hello,
    I have a code with me, please check it and see, if it helps you.
    Code:
     Pattern p = Pattern.compile("(xxxxxxx)(?=.*?\\|)");  
     Matcher m = p.mcher(test);  
     System.out.println("test :" + test);  
     StringBuffer bf = new StringBuffer();  
       
     while (m.find()) {          
     String groupStr = m.group();              
                 switch(groupStr.charAt(0)) {  
                   
                 case 'x' : m.appendReplacement(bf, "u");                 
                    break;  
                 case 'x' : m.appendReplacement(bf, "U");                 
                break;  
                 case 'x' : m.appendReplacement(bf, "a");                 
                         break;  
                 case 'x' : m.appendReplacement(bf, "A");                 
                         break;  
                 case 'x' : m.appendReplacement(bf, "o");                 
                         break;  
                 case 'x' : m.appendReplacement(bf, "O");                 
                         break;  
                 case 'x' : m.appendReplacement(bf, "B");                 
                break;  
             //default  : break;              
                 }                                      
         }  
     m.appendTail(bf);  
     System.out.println("bffer : " + bf.toString());  
     }  
     }

Similar Threads

  1. Replies: 5
    Last Post: 23-12-2010, 07:12 PM
  2. Regex to replace word
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 04:34 AM
  3. Regex replace an integer with string
    By New ID in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 01:35 AM
  4. Replace all special characters
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 01:23 AM
  5. Regex strip out all non alpha Characters from a String
    By Chhaya in forum Software Development
    Replies: 2
    Last Post: 22-05-2009, 06:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,891,257.05394 seconds with 16 queries