[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

Issue with regular expressions in java

I keep getting the following exception when i try to do a regex search in java:

java.lang.IllegalStateException: No match available
        at java.util.regex.Matcher.end(Matcher.java:372)
        at com.roeschter.jsl.DataGrab.get_value(DataGrab.java:112)
        at com.roeschter.jsl.DataLoad.run(DataLoad.java:47)

Here's my code:

String strOpenTableRegex = "<table";
      
returned_content = "xxxxxxxxxxxxxxxxxx<table>XXXXXXXXXXXXXXXXXXXXXX";
tables = 1;
      
for (int i=0;i<tables;i++)
{
      System.out.println("Compiling regex.");
      pattern = Pattern.compile(strOpenTableRegex);
            
      System.out.println("Matching regex.");
      matcher = pattern.matcher(returned_content);
            
      nCurOffset = matcher.end() + 1;

}
0
opike
Asked:
opike
  • 3
  • 2
1 Solution
 
VenabiliCommented:
This means that there is no match - check of there is one before you do the offset.
0
 
VenabiliCommented:
Simply use

boolean matchFound = matcher.matches();
if (matchFound ){
    nCurOffset = matcher.end() + 1;
}

If you want the above to match you need .*<table.* as a pattern. Or something like this - because at the moment the string does not match your pattern
0
 
opikeAuthor Commented:
I treid .*<table.*  (dot asterisk <table dot asterisk) and that didn't work with my code either.

I even simplified my example down to "table" (without the qutoes) which theoretically should work since it has no special characters, just literals. No dice.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
opikeAuthor Commented:
I figured out the problem... I was missing a matcher.find() call.
0
 
opikeAuthor Commented:
Awarding the points because of the effort to help.
0
 
gordon_vt02Commented:
I'd also move the Pattern.compile() call outside of your for loop.  There's no reason to do an expensive recompile for every line, the call to pattern.matcher() is all you need.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now