Solved

String find and Replace

Posted on 2010-09-12
8
379 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
8 Comments
 
LVL 20

Expert Comment

by:Sathish David Kumar N
Comment Utility
u need progrmatically  or manually??
0
 
LVL 20

Expert Comment

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

Expert Comment

by:gurvinder372
Comment Utility
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
 
LVL 74

Expert Comment

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

Find:

    /\*.+?\*/

Replace:

    [empty string]
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Accepted Solution

by:
rocman earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now