Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Last Index Of by Regex in Java

Tags:

java

regex

i have a string %/O^/O%/O. I want to find the last / to split the string. First attemp was: \/[POL]$ but that gets it inclusive the "O" which is obvious. Has somebody a tip?

like image 872
Andreas Avatar asked Apr 01 '10 13:04

Andreas


People also ask

How does last index of work in Java?

The lastIndexOf() method returns the position of the last occurrence of specified character(s) in a string. Tip: Use the indexOf method to return the position of the first occurrence of specified character(s) in a string.

How to find the last instance of a character in Java?

Use the lastIndexOf() method to find the last occurrence of a character in a string in Java.

How do you use last index?

Definition and Usage The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string. The lastIndexOf() method searches the string from the end to the beginning. The lastIndexOf() method returns the index from the beginning (position 0).


2 Answers

The core question is good although the example you gave doesn't need it. Java's indexOf doesn't take regular expressions. Answering just subject part of the question, here's what you would need:

/**
* Version of indexOf that uses regular expressions for the search
* by Julian Cochran.
*/
public static int indexOfRegex(String message, String toFind) {
  // Need to add an extra character to message because to ensure
  // split works if toFind is right at the end of the message.
  message = message + " ";
  String separated[] = message.split(toFind);
  if (separated == null ||
      separated.length == 0 ||
      separated.length == 1) {
    return -1;
  }
  return separated[0].length();
}

If you need the last index:

/**
* Version of lastIndexOf that uses regular expressions for
* the search by Julian Cochran.
*/
public static int lastIndexOfRegex(String message, String toFind) {
  // Need to add an extra character to message because to ensure
  // split works if toFind is right at the end of the message.
  message = message + " ";
  String separated[] = message.split(toFind);
  if (separated == null ||
      separated.length == 0 ||
      separated.length == 1) {
    return -1;
  }
  return separated[separated.length - 1].length();
}
like image 63
Julian Cochran Avatar answered Oct 08 '22 01:10

Julian Cochran


I agree that using the standard String.lastIndexOf() method is your best course of action, but I have recently had use for the Regex part (namely, I wanted to find the last non-alphanumeric character in a string).

I ended up writing it myself, and thought to share, in hopes that it would serve to help others:

/**
 * Indicates that a String search operation yielded no results.
 */
public static final int NOT_FOUND = -1;

/**
 * Version of lastIndexOf that uses regular expressions for searching.
 * 
 * @param str String in which to search for the pattern.
 * @param toFind Pattern to locate.
 * @return The index of the requested pattern, if found; NOT_FOUND (-1) otherwise.
 */
public static int lastIndexOfRegex(String str, String toFind)
{
    Pattern pattern = Pattern.compile(toFind);
    Matcher matcher = pattern.matcher(str);
    
    // Default to the NOT_FOUND constant
    int lastIndex = NOT_FOUND;
    
    // Search for the given pattern
    while (matcher.find())
    {
        lastIndex = matcher.start();
    }
    
    return lastIndex;
}

/**
 * Finds the last index of the given regular expression pattern in the given string,
 * starting from the given index (and conceptually going backwards).
 * 
 * @param str String in which to search for the pattern.
 * @param toFind Pattern to locate.
 * @param fromIndex Maximum allowed index.
 * @return The index of the requested pattern, if found; NOT_FOUND (-1) otherwise.
 */
public static int lastIndexOfRegex(String str, String toFind, int fromIndex)
{
    // Limit the search by searching on a suitable substring
    return lastIndexOfRegex(str.substring(0, fromIndex), toFind);
}

Also, it may be possible to make this method faster by first reversing the input string, then taking the ending index of the first group (rather than going over all the groups).

But to do that you would have to reverse the pattern as well; that can be simple in some cases (like my case of searching for a single character), but may prove problematic in others.

like image 37
Tomer Godinger Avatar answered Oct 08 '22 01:10

Tomer Godinger