Results 1 to 4 of 4

Thread: Regex matches last index

  1. #1
    Join Date
    May 2009
    Posts
    640

    Regex matches last index

    I use the Pattern and Matcher in JAVA for comparing strings to regular expressions. In case of error in the input string (not match) I just want to get the index of the last character which matches expression to find the problematic character in the string.

    I do not know if the question has been asked, but I do not know how to get this value: in fact these are the attributes 'last' and 'oldlast' Object match, but they are inaccessible directly.

    And when the end () method is called when the result does not match throughout the first attribute is -1 is an IllegalStateException is thrown

    Does anyone have an idea of the way to do to get this value?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Regex matches last index

    First of all do something like this:

    Code:
    int endIndex = 0;
    while (matcher.find()) {
       endIndex = matcher.end();
    }
    // More match, the index of the last match was recovered

  3. #3
    Join Date
    May 2009
    Posts
    640

    Re: Regex matches last index

    Thank you! I just have to try something like that, but it works especially well if you use a regular expression like:
    [az]*

    If we add such a constraint length: [az]{2,10}
    then of Matcher indexes are not initialized if a bad character is in position 0 of the string to test ...

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Regex matches last index

    Everything depends on what you seek. Another way is to create a regular expression like:

    [expression that, this 0 or 1 or more times] ([expression representing reverse everything that does not present 0 or 1 time])

    then just make a match that phrase from the beginning of the string and if the 2nd part, we will put in brackets, is present, then we can retrieve its index. This index is the position of the first characters that do not match.

Similar Threads

  1. Conventional MP matches in FEAR 3
    By rUChIRr in forum Video Games
    Replies: 5
    Last Post: 02-06-2011, 10:09 PM
  2. Matches in WWE SmackDown
    By Romany in forum Video Games
    Replies: 5
    Last Post: 29-03-2010, 10:28 AM
  3. Does Google decides which content to index & what not to index?
    By Uddhav in forum Technology & Internet
    Replies: 5
    Last Post: 04-03-2010, 07:37 PM
  4. Regex retrieve the value that matches
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 04:29 AM
  5. Inserting an index entry into index 0 of file 25
    By Dharmesh Arora in forum Operating Systems
    Replies: 3
    Last Post: 16-07-2009, 10:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,112,271.94458 seconds with 16 queries