2

I have a database with 50000 rows. I want to get all rows one by one, put them into a String variable and delete all characters between "(" and ")", then update the row. I just don't know how to delete all characters between "(" and ")". I want to do this with java.

javac
  • 2,397
  • 4
  • 16
  • 26
Tokenizer
  • 291
  • 1
  • 6
  • 20
  • 1
    FYI: Doing this in Java is likely to be MUCH slower than handling this with pure SQL. – Necreaux May 13 '15 at 12:11
  • When you see the light(TM), see this: http://stackoverflow.com/questions/4271186/how-can-i-use-mysql-replace-to-replace-strings-in-multiple-records – Ian2thedv May 13 '15 at 12:19

2 Answers2

9

Your regular expression would be something like:

data.replaceAll("\\(.*?\\)", "()"); //if you want to keep the brackets

OR

data.replaceAll("\\(.*?\\)", ""); //if you don't want to keep the brackets

The string consists of 3 parts:

\\(  //This part matches the opening bracket
.*?  //This part matches anything in between
\\)  //This part matches the closing bracket

I hope this helps

Voidpaw
  • 892
  • 1
  • 5
  • 16
0

Use the replaceall(string regex, string replacement) statement, like this:

data.replaceall(" and ", "");
bugs2919
  • 371
  • 1
  • 8
  • can you copy the content of the string here? – bugs2919 May 12 '15 at 17:50
  • String temp = "Bold (medium) ..."; I need to Remove (medium) and make my string like this "Bold ..." – Tokenizer May 12 '15 at 18:17
  • try with temp.replace("(medium)",""); – bugs2919 May 12 '15 at 19:57
  • I told i have 50000 string which are diffrent and i need to delete every character in this strings between "(" and ")". All i need is a code that remove all characters between 2 special character. – Tokenizer May 12 '15 at 20:19
  • String s = "ab(cd)e"; s = s.replaceAll("\\(.*?\\)", "()"); // ab()e – bugs2919 May 12 '15 at 20:22
  • Sorry, i haven´t seen that it's says BETWEEN with that regular expression (regx) should work! – bugs2919 May 12 '15 at 20:23
  • it didn't work again. I use your regx but gave me an error: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ ) I really need this :( – Tokenizer May 12 '15 at 20:39
  • 1
    @Matarata You need to escape the backslashes in java code before regex evaluation: s.replaceAll("\\\(.*?\\\)", "()"); – javac May 13 '15 at 11:21