Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String find and Replace

Posted on 2010-09-12
8
Medium Priority
?
405 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:Gurvinder Pal Singh
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

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!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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,…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

670 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