Check if a string has four consecutive letters in ascending or descending order

Stack overflow in the daytime.

I am using regex in noob, and here is my problem. I need to check the password if it contains 4 consecutive characters. So far, what I just looked at is about numbers. Here is my regex:

increasing numbers - ^.? (?: 0123 | 1234 | 2345 | 3456 | 4567 | 5678 | 6789). $

descending numbers - ^.? (?: 9876 | 8765 | 7654 | 6543 | 5432 | 4321 | 3210). $

This only works for numbers. I know that this is already an excess in regular expression, so I do not want to do this with letters. If I do this, it will be too hard.

abcdblah // true due to abcd

helobcde // true due to bcde

dcbablah // true beacause of dcba

heloedcb // true due to edcb

Any help would be greatly appreciated. Thanks stackoverflow.

+4
source share
5 answers

here is an idea that does not use regex: all characters are ansi and usually sequentially. therefore abcd had to say the following ansi values: 64.65.66.67

pseudo code:

for (i=string.start;i<string.end-4;i++) { check=string.substring(i,4); c1=check.substring(0,1); c2=check.substring(1,1); c3=check.substring(2,1); c4=check.substring(3,1); if (c1.ansival==c2.ansival+1 && c2.ansival==c3.ansival+1 && c3.ansival==c4.ansival+1) { return false; } else { return true; } } 

also repeated in reverse order (c1.ansival + 1 == c2.ansival) to decrease

+4
source

The answer is simple: do not use regular expressions.

Use this approach:

  • iterate over each letter (of course, skip the last letters of the tree)
    • iterate over the next three letters and check ascending
      • If all of them are ascending, return true.
    • iterate over the next three letters and check in descending order
      • If they all descend, return false.
  • return false

In the code, it will look like this (untested code):

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

Good luck
Stackoverflow :)

+6
source

It is not possible to solve this problem with regular expressions, except for the "overkill" solution listing each of the possible sequences that you want to match. Regular expressions are not expressive enough to offer a better solution.

+2
source

This is my decision. It uses only one loop.

Keep in mind that you will need more logic if you want to restrict it to pure ASCII.

 static boolean isWeak(String pass) { Character prev = null; Boolean asc = null; int streak = 0; for (char c : pass.toCharArray()) { if (prev != null) { switch (c - prev) { case -1: if (Boolean.FALSE.equals(asc)) streak++; else { asc = false; streak = 2; } break; case 1: if (Boolean.TRUE.equals(asc)) streak++; else { asc = true; streak = 2; } break; default: asc = null; streak = 0; } if (streak == 4) return true; } prev = c; } return false; } 
+2
source

Consider this

 String s = "aba"; for (int i = 0; i < s.length() - 1; i++) { if (!(Character.isLetter(c1) && Character.isLetter(c2))) { //reject } if ((int)s.charAt(i) > (int)s.charAt(i + 1))) { //reject } 

}

for s, the if statement will be true so you can reject it. If s was abc, then the if statement will never be true.

The above code with > checks the ascending order. Use < to decrease

0
source

Source: https://habr.com/ru/post/1435047/


All Articles