I am trying to write a generic method that will search a file for a given string and replace it with another string. I am using java regex for the same
patternMatcher = Pattern.compile(searchString);
while ((line = readLine()) != null) {
Matcher regexMatcher = patternMatcher.matcher(line);
if (regexMatcher.lookingAt()) {
line = regexMatcher.replaceAll(replaceString);
..so on
This logic works as long as the search string is in the beginning of each line in the file. otherwise the pattern matching does not occur. Can anyone please suggest a solution?
for eg. My search String is "This" and Replace string is "That"
Input file contains: This is not This funny
Output: That is not That funny
But when
Input file contains: 007 This is not This funny
Output: 007 This is not This funny