我有一个文本文件 file.txt
内容是每行四个字符:
fdsa
vffd
csfe
zfvs
gfge
fdsk
dcfa
zdco
fdau
dpkl
fpyt
这样的结构,有几十万行。我想把他们分类,带 aeoiu 任一个字符或者多个元音字符的写在
one.txt 文件中,不带 aeoiu 中的任何一个字符放在文件 two.txt 中,代码写了好久,不会做啊,正则完全不懂.有高手指教下吧。又做了伸手党,可耻啊!!!
|  |      1lxrmido      2016-01-05 20:27:18 +08:00 preg_match('/[aeoiu]/', $word); | 
|  |      2Zzzzzzzzz      2016-01-05 20:30:06 +08:00  2 用 strpbrk | 
|  |      3bdbai      2016-01-05 20:32:25 +08:00 via iPhone 正则表达式存文件好像挺难的,找个培训班老师问问吧。 | 
|  |      4liuhaotian      2016-01-05 20:34:54 +08:00 preg_match_all('/([aeiou]+)/',$word); foreach($word as $theword){ //output } preg_match_all('/(^[aeiou]+)/',$word); foreach($word as $theword){ //output } 好像是这样的吧 | 
|  |      5liuhaotian      2016-01-05 20:35:42 +08:00 错了。。 preg_match_all('/([aeiou]+)/',$word,$matches); foreach($matches as $theword){ //output } preg_match_all('/(^[aeiou]+)/',$word); foreach($matches as $theword){ //output } 好像是这样的吧 | 
|  |      6yumijie OP | 
|  |      7cnxh      2016-01-05 20:43:51 +08:00 via iPhone 是域名吗 | 
|  |      10jfcherng      2016-01-05 20:48:46 +08:00  1 $file = file_get_contents('file.txt', 'r'); $one = $two = []; $lines = explode("\n", $file); foreach ($lines as &$line) { if (preg_match('/[aeiou]/S', $line)) { // if (strpbrk($line, 'aeiou') !== false) { // if (strcspn($line, 'aeiou') < strlen($line)) { $one[] = &$line; } else { $two[] = &$line; } } $one = implode("\n", $one); $two = implode("\n", $two); file_put_contents('one.txt', $one); file_put_contents('two.txt', $two); | 
|      11ChiChou      2016-01-05 20:49:55 +08:00 grep '[aeiou]' file.txt > one.txt grep -v '[aeiou]' file.txt > two.txt | 
|  |      15jfcherng      2016-01-05 21:08:58 +08:00  1 | 
|      16movtoy      2016-01-05 21:17:33 +08:00 不会正则,笨办法。不知道能不能用 $fh = fopen('filename.txt', r) or die('errormsg'); while(! feof($fh)) { //判断是否文件末尾 $string = fgets($fh);//得到一行 //处理该行 for ($i=0; $i <strlen($string); $i++) { if (strpos('aeiouAEIOU',$string[$i]) === false) { # 写入 two.txt } else { # 写入 one.txt } } } fclose($fh); | 
|  |      17KentY      2016-01-05 21:48:38 +08:00  1 awk 很容易啊 awk '{print > (($0~/[aeiou])?"one":"two")".txt"}' file 没测试, 估计差不多. | 
|  |      18KentY      2016-01-05 21:49:15 +08:00 差个 /, : /[aeiou]/ | 
|  |      20elvba      2016-01-06 01:18:49 +08:00 array_map(function ($line) { file_put_contents(( strpbrk($line, 'aeiou') !== false ) ? 'one.txt' : 'two.txt', $line, FILE_APPEND); }, file('file.txt')); | 
|  |      21chaegumi      2016-01-06 07:49:31 +08:00 搞了几年的 php 了,我都不知道有这个函数: strpbrk | 
|  |      25yumijie OP 非常感谢楼上诸位,我就不一一 @ | 
|  |      26yumijie OP 楼上好几位的代码都能实现我想要的功能,我在查查手册,消化消化。 | 
|  |      27xiamingchong      2016-01-06 09:38:28 +08:00 其实不用 php ,两个命令就好了 awk '/[aeiou]+/ {print }' file.txt > one.txt awk '/^[^aeiou]+$/ {print }' file.txt > two.txt | 
|  |      28flydogs      2016-01-06 10:19:04 +08:00 直接用文本编辑器,正则规范 分别检索两次 不用写 php 程序把。 | 
|  |      29minongbang      2016-01-06 18:20:30 +08:00 | 
|  |      30yumijie OP @minongbang 哦我在网上找了好久没找到这样的工具. | 
|  |      31KIDJourney      2016-01-10 21:11:36 +08:00  1 | 
|  |      32yumijie OP @KIDJourney  可惜我看不懂 python PHP 我都只能看点皮毛 |