The answer is simple: don't use regexes.

Use this approach:


iterate over each letter (of course, skip the last tree letters)

iterate over the next three letters and check for ascending order

if they all were ascending return true.

iterate over the next three letters and check for descending order

if they all were descending return false.


return false


In code, this would look like this (untested code):

public boolean checkForAscendingOrDescendingPart(String txt, int l)
{
    for (int i = 0; i &lt;= txt.length() - l; ++i)
    {
        boolean success = true;
        char c = txt.charAt(i);
        for (int j = 1; j &lt; l; ++j)
        {
            if (((char) c + j) != txt.charAt(i + j))
            {
                success = false;
                break;
            }
        }
        if (success) return true;

        success = true;

        for (int j = 1; j &lt; l; ++j)
        {
            if (((char) c - j) != txt.charAt(i + j))
            {
                success = false;
                break;
            }
        }
        if (success) return true;
    }
    return false;
}


Good luck!
StackOverflow :)

check if a string has four consecutive letters in ascending or descending order
Why in every textbook when deriving Dirac's equation the smallest possible matrices ($2 \times 2$) are used?
I wonder why one couldn't use spin 1 matrices ($3 \times 3$) and get relativistic equation for spin 1 particle?
