Find the highest number between two literal strings using a Regular Expression

What is the best way to use regex to get the highest number in a group of strings that match a certain pattern.
For example:

Suppose I wanted to find the next integer to suffix an Untitled file.

Here is an an example of already existing file names:

  • -> should match
  • -> should match (case insensitive)
  • -> should not match (does not contain
  • -> should not match (does not exactly match

In this example the function below should return: 3

    public int getNextUntitledFileSuffix(String[] fileNames){
      int nextSuffix = 1;
      int maxSuffix = 1;
      for (int i = 0; i < fileNames.length; i++){
        //use regex to set nextSuffix
      return nextSuffix;

Source: regex

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.