0

I have a collection of files from multiple sources.

Each file contains strings like:

File 1: A) B) C) D) E) 
File 2: a) b) c) d) e)
File 3: a. b. c. d. e.
File 4: a- b- c- d- e-
(...)

I know I could code all possible patterns beforehand, but I'd rather make it automatically.

Is it possible to make a program to read the file and figure the pattern? Ex:

File 1: A) B) C) D) E) # => [ABCDE]\)
File 2: a) b) c) d) e) # => [abcde]\)
File 3: a. b. c. d. e. # => [abcde]\.
File 4: a- b- c- d- e- # => [abcde]-
Victor Ribeiro
  • 577
  • 7
  • 20
  • Depends on what you want to do [here](http://www.regexformat.com/version_files/Rx5_ScrnSht01.jpg) and [here](http://www.regexformat.com/Dnl/_Samples/_Ternary_Tool%20(Dictionary)/___txt/). Creates a regex trie via ternary tree. –  Mar 28 '16 at 22:28
  • Possible duplicate: http://stackoverflow.com/questions/616292/is-it-possible-for-a-computer-to-learn-a-regular-expression-by-user-provided-e – Amadan Mar 29 '16 at 01:33
  • Read http://stackoverflow.com/a/5395777/128421. You should be able to build something based on it. – the Tin Man Mar 29 '16 at 02:40

1 Answers1

1

Regexp.union is smart enough to escape the parens, but that is it.

str = "A) B) C) D) E)"
p re = Regexp.union(*str.split) # => /A\)|B\)|C\)|D\)|E\)/

Perl's Regexp::assemble might be able to do this, AFAIK there is no Ruby equivalent.

steenslag
  • 79,051
  • 16
  • 138
  • 171