'Regex: match if any n-character combination is present

Is it possible to use regex to match all strings which contain any n-character combination, please?

For example, I have a string such as 1212789 and want to match any 4-character combination of these numeral, therefore:

  • 1212656 – true
  • 1265612 – true
  • 1279326 - true
  • 123455 - false
  • 121654 - false

If this is possible using regex, how?

Thank you!



Solution 1:[1]

Check if pattern exits

The following regex can help you get true/false results (identify if a given pattern exists in a string). For the pattern '1234':

1.*2.*3.*4

For a given string, it will match everything that comes between the 4 digit pattern, essentially verifying the pattern exits in this string:

  • 192934076
  • 12034001234098723

Check number of occurrences of the pattern

Simply add ? to make the regexp only match one occurrence

1.*?2.*?3.*?4

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1