• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

matching a string in a line  RSS feed

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to read a file and looking for a specific word "drugs" in a line and trying to replace the word by "medicine"
-------------------------------------------------------------------------
try {
BufferedReader in = new BufferedReader(new FileReader(objTemplateFile));
String str;
while ((str = in.readLine()) != null)
{
System.out.println(str);
if (str.matches("DQLString"))
{
System.out.println("we have a match");
}
}
in.close();
} catch (IOException e) {}
--------------------------------------------------------------------

but i think so "str.matches" matched the entire line and not a word in the line. Can some one tell how to find a word in a line and replace that word with a new word.

Any help would be appreciated

Raj
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the various "replace" methods in String?
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Raj kalaria
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public final class tm {
private static String REGEX = "upper('%test%')))";


private static String INPUT = "DQLString=SELECT object_name,r_creation_date FROM library_object WHERE (((upper(library_type) = upper('Posters')) and (upper(product_name) like upper('%test%')))"
private static String REPLACE = "CIPLA";

public static void main(String[] argv) {
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // get a matcher object
INPUT = m.replaceAll(REPLACE);
System.out.println(INPUT);
}
}

----------------------------------------------

can some oneplease helpwhy my string is not being replaced ( I am trying to replace upper('%test%'))) with Cipla

it throws mw the error
Unmatched closing ')' near index 14
upper('%test%')))
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!