File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Problem with FileWrite and Regular Expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem with FileWrite and Regular Expression" Watch "Problem with FileWrite and Regular Expression" New topic
Author

Problem with FileWrite and Regular Expression

Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Hi everyone...
I'm trying to filter out a hashed password from a file, by reading the file line by line using regex and matcher.group()
and then save the fetched groups in a new file...


The problem is that it keeps giving me this Exception:
java.lang.IllegalStateException: No match found
So,the file is full of line like this:
user227:4ec444a1aa461af137b3846b0f50a86fe1858da7:10063:0:99999:7:::

So please help me out with this weird Exception!?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

The group() method returns the result from a match... but you didn't tell the regex to match anything. You need to actually perform a match (and succeed) before you can get the result from it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Henry Wong wrote:The group() method returns the result from a match... but you didn't tell the regex to match anything. You need to actually perform a match (and succeed) before you can get the result from it.

Henry


but this what happens in line 13, doesn't it?
and in line 3 where : FileReader fr = new FileReader(f);//f ="list" which lines of the same of what mentioned above...
and I tested the validity of the match, and it worked well, but when it comes inside "while" and with "write" it starts to play around!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Abdulmalik Malik wrote:
but this what happens in line 13, doesn't it?


No. Line 13 just creates a Matcher instance, from the Pattern instance. This is *not* telling the regex engine to perform a match -- that is done with the matcher instance (using either the matches() or find() method).

Perhaps, the regex javadoc is a good place to start here.

Henry
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Henry Wong wrote:
Abdulmalik Malik wrote:
but this what happens in line 13, doesn't it?


No. Line 13 just creates a Matcher instance, from the Pattern instance. This is *not* telling the regex engine to perform a match -- that is done with the matcher instance (using either the matches() or find() method).

Perhaps, the regex javadoc is a good place to start here.

Henry


Thank you very much for helping..

I agree with what you have said line 13, but what I know -and please correct me if I'm mistaken- that matcher.group(2) returns the matched String, so what's wrong with my code?
Especially that I tried other methods than (group) like groupCount() and it did work!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Abdulmalik Malik wrote:
I agree with what you have said line 13, but what I know -and please correct me if I'm mistaken- that matcher.group(2) returns the matched String, so what's wrong with my code?


The group() method does "returns the matched String", but you need to actually perform the match first -- which you are not doing.

Abdulmalik Malik wrote:
Especially that I tried other methods than (group) like groupCount() and it did work!


The groupCount() method returns the number of groups in the pattern -- not the number of groups in a match. It does *not* require that a match be performed, for it to work.

Henry
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Thanks again, and sorry for being a pain!
but I tried the following, and it worked as well:
.
.
.

.
.
and I had a String printed!
so, how come it doesn't work when I put it inside a while loop?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Abdulmalik Malik wrote:Thanks again, and sorry for being a pain!
but I tried the following, and it worked as well:


I don't know what else to tell you. It shouldn't work.


But I would like to see this (complete and compilable) working code. Can you post it?

Henry
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Henry Wong wrote:
Abdulmalik Malik wrote:Thanks again, and sorry for being a pain!
but I tried the following, and it worked as well:


I don't know what else to tell you. It shouldn't work.


But I would like to see this (complete and compilable) working code. Can you post it?

Henry


yes sir,here you go,here is the complete code

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

You said that there was a call to group() that worked, where is that?

Henry
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Henry Wong wrote:You said that there was a call to group() that worked, where is that?

Henry

Sorry, I've been sleeping!
this is code that worked:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Abdulmalik Malik wrote:

And there is the line that makes it work - an actual search in the String for the regex. As Henry already said
You need to actually perform a match (and succeed) before you can get the result from it.
that is done with the matcher instance (using either the matches() or find() method).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Rob Prime wrote:
Abdulmalik Malik wrote:

And there is the line that makes it work - an actual search in the String for the regex. As Henry already said
You need to actually perform a match (and succeed) before you can get the result from it.
that is done with the matcher instance (using either the matches() or find() method).



It works now, I really appreciate you help guys, thank you very very much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with FileWrite and Regular Expression