0

I am trying to replace only matched strings in a file by using matcher.replaceAll().Like below

public static void main(String[] args) throws IOException, BadLocationException {

      FileInputStream fis = null;
      String output = new Scanner(new File("C:/file.in")).useDelimiter("\\Z").next();//file.in contains HTML text
      String s2 = null;
      StringWriter writer = new StringWriter();
      for (int i=0; i<patternList.size(); i++)//PatternList contains names to be extracted from file
      {
          Matcher matcher = patternList.get(i).matcher(output);
          while (matcher.find()) 
          {
                String matched = matcher.group();//I need to replace only matched strings returned by matcher
                s2=matcher.replaceAll("<span style='background-color:red;'>"+matched+"</span>");
      File file = new File("C:/data/filename.in");
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(s2);
        bw.close();
          }   
      }

The resulting string s2 updated with only last string in PatternList.Each time i am overwriting the string with newly matched string. How can i get the final big string which is updated with all matched strings(names in patternlist).

2 Answers 2

1

I believe you have lot of redundant code. You definitely don't need a list of Pattern objects.

Consider this code:

String output = new Scanner(new File("C:/file.in")).useDelimiter("\\Z").next();
String repl = output.replaceAll("\b(Shannon|Sperling|Kim|Tammy|Nancy|Lana)\b", 
              <span style='background-color:red;'>$1</span>"); 

File file = new File("C:/data/filename.in");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write( repl );
bw.close();
Sign up to request clarification or add additional context in comments.

10 Comments

I have just showed 6 strings in the example for simplicity, In reality i have thousands of strings to be updated.That can be achieved only through "matcher". Whenever the match occurs, i need to update it.
If you have thousands of keywords even then building a fewer Pattern objects is possible by putting an | between them (as shown above) rather than creating one Pattern/Matcher for each keyword.
Those strings are stored in dictionary, some times they may be multiples of 1000's.I am reading the entry in a dictionary, building a pattern then checking for existance in a file.The way you said does not look like good way.Infact it is not at all possible to get all strings and put it there with "|" as seperator.
Every dictionary of words can return your String words so not sure what you're talking about. In your question you have String[] names. Think of reasons why your question is NOT getting any answers.
Your code works fine for replacing strings in a specified file.Because you are replacing all strings in one line and assigning to the result string.But my requirement is different.First i have to get the dictionary entry, build a pattern and use for matcher to get the matched string with START and END index(Which are very important for my project), then just highlight the matched strings in a file.The reason why i should i use Dictionary is they will change constantly.Suppose if i have 3000 names in a dictionary, how it works with your code.
|
0

I am able to solve the problem after changing s2 to output.Assign the result(matcher.replaceAll) to output instead of s2. Now all the matched strings replaced with color tag.

public static void main(String[] args) throws IOException, BadLocationException {
FileInputStream fis = null;
  String output = new Scanner(new File("C:/file.in")).useDelimiter("\\Z").next();//file.in contains HTML text
  String s2 = null;
  StringWriter writer = new StringWriter();
  for (int i=0; i<patternList.size(); i++)//PatternList contains names to be extracted from file
  {
      Matcher matcher = patternList.get(i).matcher(output);
      while (matcher.find()) 
      {
            String matched = matcher.group();//I need to replace only matched strings returned by matcher
            output=matcher.replaceAll("<span style='background-color:red;'>"+matched+"</span>");
  File file = new File("C:/data/filename.in");
  FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(output);
    bw.close();
      }   
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.