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
Solved

String find and Replace

Posted on 2010-09-12
8
386 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 29
jdbc error in jsp application 20 63
Java program running SQL query 5 37
Java Eclipse Loop 3 20
by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

791 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