• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with FileWrite and Regular Expression

 
Abdulmalik Malik
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abdulmalik Malik
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said that there was a call to group() that worked, where is that?

Henry
 
Abdulmalik Malik
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Abdulmalik Malik
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic