2

Let's say I have the string:

"12345"

Is it possible to write a regex that would have the following match results?

["1","2","3","4","5","12","23,"34","45","123","234","345","1234","2345","12345"]

I have seen some similar problems, but not exactly the same. What i wanted is a regex that can extract any possible number from strings.

  • 2
    I do not think it's suitable for regex. You can use any popular programming language to get such list. – PM 77-1 Jan 17 '23 at 16:08
  • I mean using lazy search, "\d+" would return ["1","2","3","4","5"], but using greedy search, it would return ["12345"], I guess I almost want many levels of greediness maybe? – Lucas Andrade Jan 17 '23 at 16:10
  • So do you want to get all permutations that are in order? – jvx8ss Jan 17 '23 at 16:35
  • Yes, but I dont think regex will work, by what other people are saying. – Lucas Andrade Jan 17 '23 at 17:54
  • Overlapping could be captured [inside a lookahead](https://stackoverflow.com/questions/11430863/how-to-find-overlapping-matches-with-a-regexp). Afaik you can'get get more than one match per position besides doing something like e.g. [`(?=(.....)?)(?=(....)?)(?=(...)?)(?=(..)?).`](https://regex101.com/r/wz4atV/1) which works for strings not longer than your example. – bobble bubble Jan 17 '23 at 19:45
  • [Here is a PHP demo](https://tio.run/##RY/dCoMwDIXv@xRBBq0gyv6uNvF2l75BEak/rLMlTRl7ehc3N3MRDkn4zokf/Dxfq/pWC7ELhFBCsj8cT@fkIkRRwNPhPUDnEHg5Tn2A6IEcnMGaqadBeDS9fjTUDrqxVslCVaXKl0qr9Kc3@Vcs8kJmsJhyd5FSdtyhCdESp2gQm5fuRksGVctoHYNB3cWp1Z@dkt@Th8HeyBWRgWQeR5NMCw5JrcQF7vkB0riN5vkN) – bobble bubble Jan 18 '23 at 13:16

1 Answers1

1

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'
]
Agan
  • 447
  • 2
  • 12
  • Cool, I guess I will then do the regex "\d+" to find the numbers, then apply the first approach on the matches results. – Lucas Andrade Jan 17 '23 at 18:38