• Post Reply Bookmark Topic Watch Topic
  • New Topic

illegal state exception from regex  RSS feed

 
john thatcher
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If body is the string "{oli22} potatoes are cool" anyone know why this code returns an illegal state exception: no match found when either of the group(1) begin to look for their patterns. Thanks for you help.


private static final Pattern BRACE_END = Pattern.compile(".*\\}");
private static final Pattern EGGS = Pattern.compile("\\{(.*)\\}");
private static final Pattern BACON = Pattern.compile("\\}(.*)");

public class jibbles {

public boolean workdamnyou(String body, Data data){
if (body.startsWith("{")){
if (BRACE_END.matcher(body).lookingAt()){
eggs = EGGS.matcher(body).group(1);
bacon = BACON.matcher(body).group(1);
}
}
}
}
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john thatcher wrote:If body is the string "{oli22} potatoes are cool" anyone know why this code returns an illegal state exception: no match found when either of the group(1) begin to look for their patterns. Thanks for you help.


private static final Pattern BRACE_END = Pattern.compile(".*\\}");
private static final Pattern EGGS = Pattern.compile("\\{(.*)\\}");
private static final Pattern BACON = Pattern.compile("\\}(.*)");

public class jibbles {

public boolean workdamnyou(String body, Data data){
if (body.startsWith("{")){
if (BRACE_END.matcher(body).lookingAt()){
eggs = EGGS.matcher(body).group(1);
bacon = BACON.matcher(body).group(1);
}
}
}
}


For the group() method to work (which returns found matches), you actually need to perform a search (such as using the find() or matches() methods).

Henry
 
john thatcher
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

For the group() method to work (which returns found matches), you actually need to perform a search (such as using the find() or matches() methods).

Henry


Thanks for the quick reply. I tried this but still got the same error:

If body is the string "{oli22} potatoes are cool"

private static final Pattern BRACE_END = Pattern.compile(".*\\}");
private static final Pattern EGGS = Pattern.compile("\\{(.*)\\}");
private static final Pattern BACON = Pattern.compile("\\}(.*)");

public class jibbles {

public boolean workdamnyou(String body, Data data){
if (body.startsWith("{")){
if (BRACE_END.matcher(body).lookingAt()){
if (EGGS.matcher(body).find()){
eggs = EGGS.matcher(body).group(1);
}
if (BACON.matcher(body).find()){
bacon = BACON.matcher(body).group(1);
}
}
}
}
 
john thatcher
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what was wrong but I got some inspiration and decided to use indexOf and substring instead.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john thatcher wrote:
Thanks for the quick reply. I tried this but still got the same error:

If body is the string "{oli22} potatoes are cool"

private static final Pattern BRACE_END = Pattern.compile(".*\\}");
private static final Pattern EGGS = Pattern.compile("\\{(.*)\\}");
private static final Pattern BACON = Pattern.compile("\\}(.*)");

public class jibbles {

public boolean workdamnyou(String body, Data data){
if (body.startsWith("{")){
if (BRACE_END.matcher(body).lookingAt()){
if (EGGS.matcher(body).find()){
eggs = EGGS.matcher(body).group(1);
}
if (BACON.matcher(body).find()){
bacon = BACON.matcher(body).group(1);
}
}
}
}


You got the same error because you did almost the same thing.... in this second case, you created a matcher to do the search, and when you found something, you created a new matcher (which you haven't use to search yet) and asked it for the result. Since this new matcher was never used to search, obviously, there are no results to return.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john thatcher wrote:I don't know what was wrong but I got some inspiration and decided to use indexOf and substring instead.


Yeah. That is a very good idea. you should never use something that you don't understand. At best, you can't get it working, and at worst, you do get it working but don't know how it works (or if it works correctly for all cases).

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!