• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with FileWrite and Regular Expression

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic