2016-03-28 38 views
0

我有来自多个来源的文件的集合。是否可以读取字符串的集合并返回Regexp?

每个文件都包含像字符串:

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- 
(...) 

我知道我可以事先编写所有可能的模式,但我宁愿自动进行的。

是否可以让程序读取文件和的模式? 例如:

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]- 
+0

取决于你想要做什么[这里](http://www.regexformat.com/version_files/Rx5_ScrnSht01.jpg)和[这里](http://www.regexformat.com/Dnl/_Samples/_Ternary_Tool %20(词典)/ ___ TXT /)。通过三元树创建正则表达式树。 – sln

+0

可能的重复:http://stackoverflow.com/questions/616292/is-it-possible-for-a-computer-to-learn-a-regular-expression-by-user-provided-e – Amadan

+0

阅读http:///stackoverflow.com/a/5395777/128421。你应该能够基于它建立一些东西。 –

回答

1

Regexp.union是足够聪明逃避括号,但就是这样。

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

Perl的Regexp::assemble也许能够做到这一点,据我所知是没有的Ruby等价的。

相关问题