The regular expression for this specific output would not be possible, as regular expressions are used to match patterns within a string, not to generate substrings of a given string.
You can do it with python:
input = "12345"
result = [input[i:i+j] for i in range(len(input)) for j in range(1, len(input)-i+1)]
print(sorted(result))
Output: ['1', '12', '123', '1234', '12345', '2', '23', '234', '2345', '3', '34', '345', '4', '45', '5']
Process finished with exit code 0
or JavaScript:
let input = "12345";
let result = [];
for (let i = 0; i < input.length; i++) {
for (let j = 1; j <= input.length - i; j++) {
result.push(input.slice(i, i+j));
}
}
console.log(result.sort());
Output: [
'1', '12', '123',
'1234', '12345', '2',
'23', '234', '2345',
'3', '34', '345',
'4', '45', '5'
]
Or if you want it to look like regex. So funny letters that lead to success, hardly anyone knows how and why, then just like that :D
const c = s => [...Array(1 << s.length)].map((, i) => [...s].filter((, j) => i & (1 << j)).join(''));
console.log(c("12345"));
Output:
[
'', '1', '2', '12',
'3', '13', '23', '123',
'4', '14', '24', '124',
'34', '134', '234', '1234',
'5', '15', '25', '125',
'35', '135', '235', '1235',
'45', '145', '245', '1245',
'345', '1345', '2345', '12345'
]