• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Regex program for file content.  RSS feed

 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A java program with "find and replace behaviour".

It has to take the entire file and it has to replace the string in the file that matches the regular expression with another string.

Result can be with in the file or any new file.

I don't know much of Regex API for java, please can anybody attempt...

A file can have huge amount of data, Is that Ok to take it in a String?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Numerous examples of how to use the java.util.regex package can be found here.

When dealing with a file, it's probably best to process it line by line, assuming that the file has line breaks and that no text to be replaced straddles line boundaries.
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to do the replacement with in the file.. i.e with in that file instead of new file
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really. What's the issue with creating a new file, and then deleting the old one after you're done?
 
Shake Sajan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends,

I am not much familiar with regex and sorry to say that I have not enough time to go through regex tutorial...
I have a question, like;
I need to find out the files in a directory starting and ending with some string.
start string, filename: "Abc2009"
end string, extn: ".dat"

And in the directory, I expect only one file (in most scenarios).

Now how I do is;
File f = new File(filename + "*" + extn); // Not working. f.exists() returns false.
File f = new File(filename + "[{*}]" + extn); // Not working. f.exists() returns false.

How to use the regular exp which shows anything after filename???

I also used FileNameFilter in the parent dir. Then am getting files. But I would like to do it using regular exp so that i can directly get the file.

Please respond..


Thanks and regards.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't hijack threads with something that basically is a new question, especially if the original thread is over a year old.

You say you do not want to go through a regex tutorial. Then I am sorry to say that you are unlikely to receive and answer (that you will understand). We ask of you to show some effort and do your own homework; we are not a code mill.

That said, if you want to look for files then a FileFilter or FilenameFilter is the way to go. Your code needs to check all files in a folder to see if they match, and that is just what File.listFiles() and File.list() will do if you provide a proper filter. Especially if you need all these files you will need these methods; how are you going from one File object to a list of files otherwise?
 
Shake Sajan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how I do:

final String fn = "Abc2009";
final String ex = "dat"
File ff = new File(<directory path>);
File[] files = ff.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name)
{

return name.startsWith(fn) && name.endsWith("." + ex) && (name.length() > (fn.length() + ex.length() + 1));
}});
System.out.println("no. of files: " + files.length);


But the return statement doesnt seem to be good enough, means its just using some calculation rather that not using standard api (regex).

[Its not like I am not working. I am working parallely to findout the solution. But I felt like, if I go thr the complete regex api, it will take time for me...
And also I thougt, ppl who are experienced would be reading this forum and would be giving any solution...]

Thanks for the response...
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code probably works if you want case sensitive matching. It doesn't use regex but frankly, you don't need it for something this simple. If you still want to use a regex, all you need to do is change the body of the accept method. Take a look at java.util.regex.Pattern for how to replace it. You don't need a Pattern object, String.matches will probably be enough, but that Javadoc page does explain the Java regex "language".
 
Shake Sajan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you friends for the response.

I got the solution for that.

As Rob mentioned, I can do it in diff ways. And found that String matches is the easiest and lightest way.
And I could able to get the proper regular expression.

like String filePattern = "(" + fn + ")[\\w]*(." + ex + ")";
and name.matches(filePattern);

It works perfectly.

I can do it also using Pattern and Matcher classes. But as it is simple I opted out creating more objects...


Anyways thanks for all the responses...
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.matches actually uses Pattern and Matcher objects internally.

There is one small mistake in your code. The dot has a special meaning in regexes so you will need to escape it, otherwise your results may be a bit different than you expected.
 
Shake Sajan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ouch!!!

I was not knowing that String matches used Pattern-Matcher classes. Thanks for sharing info. Then I feel its better to use Pattern-Matcher classes...

From some examples and tut, I also got to know there is some significance for '.' in regex. But since my code works fine, i did not checked for that.
And also i thought if we give inside brackets '()', it wont make any problem.

Let me correct it to: "(" + fn + ")[\\w]*(\\." + ex + ")";

I hope now it would work fine. Please let me know any problem..

Thanks once again...
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your regex will work*, but it has some redundancy in it. The two pairs of parentheses () will create a group that you don't use or need, therefore these can be removed. \\w is a character class by itself so there is no need to wrap it in [], so these can be removed as well. Combined with the escaping of any possible regex meta-characters in fn and ex the following will be what I would probably use:
The result is the same as what you wanted:
- fn in its exact form
- 0 or more word characters
- a dot
- ex in its exact form


* unless fn or ex contains regex meta-characters, but I've just given a solution for that.
 
Shake Sajan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thats awesome...
I just could not learn regex in that much depth. I found a regex details in one site, tried expecting the result.

Anyways thanks again Rob.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!