Solved

String find and Replace

Posted on 2010-09-12
8
385 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

832 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