Solved

String find and Replace

Posted on 2010-09-12
8
390 Views
Last Modified: 2013-11-23
I want to do find and replace. For example we have a string with something like .
"  select * from table a, table b where
a.id = b.id
/*<input>
and
a.input = ?
<input>*/   "

Now i want tofind all /*<input>  and <input>*/ and replace it with blank space or remove it. Some time we also  have to removemutilple tag. maybe also remove /*<account> with input also.
0
Comment
Question by:shetty01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 33657288
u need progrmatically  or manually??
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 33657295
Manualy means go to file open that give find replace!
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33657302
String str="select * from table a, table b where a.id = b.id /*<input> and a.input = ? <input>*/  ";
System.out.println(str.replaceAll("<input>","\\s"));
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33657393
I would think you would do something like:

Find:

    /\*.+?\*/

Replace:

    [empty string]
0
 
LVL 2

Accepted Solution

by:
rocman earned 500 total points
ID: 33658447
Hi shetty01,

Assuming that you want to remove the pair of matching tags only (i.e. leaving the string "and
a.input = ?"), and that you are manipulating the strings in Java ...

// Remove "/* <TAG>" and "<TAG> */" from the input line ...
Pattern tagStart = Pattern.compile("/\* *<TAG>");  // Note that "/\*" is slash, backslash, star
Pattern tagEnd = Pattern.compile("<TAG> *\*/"); // and " *\*/" is space, star, backslash, star, slash

Matcher matcher;
// Loop over all your lines
...
    String nextLine = ...
    //
    String newLine;
    matcher = tagStart.match(nextLine);
    newLine = matcher.replaceAll("");
    matcher = tagEnd.match(newLine);
    newLine = matcher.replaceAll("");
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33660548
Is it the same question?!
>>>
Does the /*<input> and <input>*/ are considered to be something like start and end tag? I think they are, so try this, because '*' must be avoided in this way :
System.out.println(str.replaceAll("/\\*<input>","").replaceAll("<input>\\*/",""));
0
 

Author Comment

by:shetty01
ID: 33664757
what should be the way if i have more than one tag to remove. and i dont know what tag to remove.I am looping thry some map. and based on the key, value, i have to remove all of those from the string and create new string and return new string.
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33665880
>> what should be the way if i have more than one tag to remove...
Then you have to "create" your start and end of the tag in this way:
String start = "/\\*" + tagNameTakenFromTheLoop;
String end = "\\*/" + tagNameTakenFromTheLoop;
Then use just the same line I proposed in comment #33660548
System.out.println(str.replaceAll(start,"").replaceAll(end,""));
I think that this is the simplest solution.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
printf performancy 11 105
JavaScript Regex not working for 1-3 digit number?   /^\d{1,3}$/ 5 69
Regex - Capture everything between two strings. 8 46
Java List 4 76
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question