0

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

4 Answers 4

1

Shouldn't it be...?

patternMatcher = Pattern.compile(searchString);
while ((line = readLine()) != null) {
    Matcher regexMatcher = patternMatcher.matcher(line);
       while (regexMatcher.find()) {
          line = regexMatcher.replaceAll(replaceString); 

Take into account that the quatifier may affect the results, perhapaps the search string should be "(this)+" or "(this)+?".

Sign up to request clarification or add additional context in comments.

1 Comment

@user1384205 You must upvote the answers that work for you and choose one as your final answer.
0

If you're searching for a constant string and not for a pattern, there's a number of reasons why you shouldn't use regex:

  • The user might type in some character that has a special meaning in regex grammar.
  • Regular expressions are slow compared to substring searching.
  • You don't want to allow the user more features (using regex matching) than you intend to.

Use String.indexOf and/or String.replace instead.

while ((line = readLine()) != null)
    if (line.indexOf(searchString) != -1 )
        line.replace(searchString, replaceString);

2 Comments

The files im working with sometimes run to several MBs and the application is very io intensive. The reason I decided to use regex against string manipulation was the same since i'd be reading the stream instead of reading it line by line. Please correct me if im worng.
If you want good IO performance, have a look at java.nio (new I/O). If you want to read a line with NIO, use BufferedReader.readLine() on the NIO stream. All this has nothing to do with whether you'll choose to use regex or not.
0

I'm not familiar with Java, but as per the docs, lookingAt looks at the beginning of the string. I would just skip looking for the match and blindly run replaceAll regardless of whether there is a match; it will replace nothing if there is no match.

If for some reason you need to look for a match before attempting to replace (which is wasteful), the correct function is find. See http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Matcher.html

2 Comments

Thanks a ton. lookingAt was a wrong usage in my case. find works well.
Keep in mind that String.replaceAll() compiles a regex out of the search string. If you don't need this, use String.replace().
0

If memory is not an issue, you can read the entire file as String and use public String replaceAll(String regex, String replacement) in String API.

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.