2

I want to generate RegEx Pattern from a given matches
for example i want to get <b>\d+<\b> from the following array of matches

<b>1</b>
<b>2</b>
<b>3</b>
<b>4</b>
<b>5</b>
...

any ideas?

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
MrBassam
  • 349
  • 1
  • 5
  • 17
  • 2
    What types of patterns could you get? What forms could the given matches take? – midgetspy Aug 22 '12 at 06:35
  • I just want to generate RegEx from a given array of matches as string[], Matches could by any block of chars, Can't expect the format, Thank you – MrBassam Aug 22 '12 at 06:40
  • Your "desired regex" is probably wrong - don't you want "\d+" (\b is word boundary)... +1 for interesting question, also likly way outside of SO scope :). – Alexei Levenkov Aug 22 '12 at 07:04
  • 1
    No way to do it, cause on this sample there could be more than one regex pattern that will match all rows. To solve this issue should be additional criterias. – user854301 Aug 22 '12 at 07:06
  • @user854301 Agree, I want the nearest one :) – MrBassam Aug 22 '12 at 07:12
  • @AlexeiLevenkov Thank you for the +1, I have test the regex and it works, `\d` for any digit – MrBassam Aug 22 '12 at 07:14
  • Possible duplicate of [Is it possible for a computer to "learn" a regular expression by user-provided examples?](https://stackoverflow.com/questions/616292/is-it-possible-for-a-computer-to-learn-a-regular-expression-by-user-provided-e) – Anderson Green Feb 10 '18 at 20:13

2 Answers2

4

Tool

From an answer to a similar question, the link below may help you:

txt2re: Online regular expression generator.

Understanding

If you search for deeper understanding of the topic instead of a quick solution, you should read answers to following question: Is it possible for a computer to “learn” a regular expression by user-provided examples?

Coding

If you want to code a quick solution yourself, this answer worths to look.

Note

As indicated with the meaningful joke, don't forget that computers cannot read your mind, and several different regexes can be generated for the same input.

Community
  • 1
  • 1
mmdemirbas
  • 9,060
  • 5
  • 45
  • 53
2

A Genetic Programming (GP) based solution to your problem: Regex Generator++. It takes some examples as input, runs a machine learning algorithm and produces as output the regex which matches the examples.

Internally, the system tries to solve the problem by means of an evolutionary algorithm, a computational paradigm inspired by biological evolution. It generates a number of possible candidate solutions (in this case: a number of regexes) at random and evaluates, for each solution, a fitness measure, a performance index of how good is the candidate solution.

There are different types of evolutionary algorithms, in this case (GP) the solutions are represented as abstract syntax trees.

You can find more details here or in the related papers

mimmuz
  • 339
  • 4
  • 9
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Notlikethat Dec 21 '14 at 18:51
  • @Notlikethat you are right, I'll insert the details here as soon as possible! – mimmuz Dec 21 '14 at 19:21