This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

matching a string in a line

 
Raj kalaria
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
 
marc weber
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?
 
Ken Blair
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