PHP 用正则表达式怎样实现这个功能?

2016-01-05 20:11:06 +08:00
 yumijie

我有一个文本文件 file.txt
内容是每行四个字符:
fdsa
vffd
csfe
zfvs
gfge
fdsk
dcfa
zdco
fdau
dpkl
fpyt

这样的结构,有几十万行。我想把他们分类,带 aeoiu 任一个字符或者多个元音字符的写在
one.txt 文件中,不带 aeoiu 中的任何一个字符放在文件 two.txt 中,代码写了好久,不会做啊,正则完全不懂.有高手指教下吧。又做了伸手党,可耻啊!!!

3076 次点击
所在节点    PHP
32 条回复
lxrmido
2016-01-05 20:27:18 +08:00
preg_match('/[aeoiu]/', $word);
Zzzzzzzzz
2016-01-05 20:30:06 +08:00
用 strpbrk
bdbai
2016-01-05 20:32:25 +08:00
正则表达式存文件好像挺难的,找个培训班老师问问吧。
liuhaotian
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
}
好像是这样的吧
liuhaotian
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
}
好像是这样的吧
yumijie
2016-01-05 20:41:56 +08:00
@Zzzzzzzzz

@liuhaotian

@lxrmido
非常感谢楼上几位,我去试试.......
cnxh
2016-01-05 20:43:51 +08:00
是域名吗
yumijie
2016-01-05 20:43:55 +08:00
@bdbai 感谢,身边没有懂程序的朋友,只好来网上找
yumijie
2016-01-05 20:44:54 +08:00
@cnxh 对的,我想筛选域名
jfcherng
2016-01-05 20:48:46 +08:00
$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);
ChiChou
2016-01-05 20:49:55 +08:00
grep '[aeiou]' file.txt > one.txt
grep -v '[aeiou]' file.txt > two.txt
yumijie
2016-01-05 21:04:11 +08:00
@jfcherng 非常感谢,尝试了下,可以用。
yumijie
2016-01-05 21:04:55 +08:00
@ChiChou 感谢回复,你这个实在 linux 下使用吗?
yumijie
2016-01-05 21:06:39 +08:00
@jfcherng

$one = $two = []; 这句什么意思?创建数组吗?
jfcherng
2016-01-05 21:08:58 +08:00
@yumijie http://php.net/manual/en/migration54.new-features.php
PHP 5.4 之後可以用 [...] 替代 array(...)
movtoy
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);
KentY
2016-01-05 21:48:38 +08:00
awk 很容易啊

awk '{print > (($0~/[aeiou])?"one":"two")".txt"}' file

没测试, 估计差不多.
KentY
2016-01-05 21:49:15 +08:00
差个 /, :
/[aeiou]/
ChiChou
2016-01-05 22:26:11 +08:00
@yumijie Linux 和 Mac 都可以。如果是 Windows 的话,可以装个 babun
elvba
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'));

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/248584

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX