免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3203 | 回复: 13
打印 上一主题 下一主题

perl正则问题 关于`字符 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-05 22:06 |只看该作者 |倒序浏览
看到有书上这样写的perl能提取数据库的一张表
#!/usr/bin/perl -wn
BEGIN { $table = shift @ARGV }
print if /^create table $table\b/io .. /^create table (?!$table)\b/io;

不过现在用mysqldump备份,数据表的名字两边都会加上``我就是不知道这`在perl重怎么匹配,以及.. 在这里的意思

我试过用print if /^DROPTABLE IF EXISTS \`$table\`;\b/io .. /^DROPTABLE IF EXISTS  \`(?!$table)\`;\b/io;

论坛徽章:
0
2 [报告]
发表于 2009-05-05 22:21 |只看该作者
print if m|^-- Table structure for table `$table\b|io ..
m|/\*!40000 ALTER TABLE `$table` ENABLE KEYS \*/\;|io ;
用这个正则可以实现我要的功能,但是`好像不需要替换,;需要替换,修改了一下代码
print if /^DROPTABLE IF EXISTS `$table`\;\b/io .. /^DROPTABLE IF EXISTS  `(?!$table)`\;\b/io;
感觉还是不可以,我这个perl的正则问题出在什么地方了呢

论坛徽章:
0
3 [报告]
发表于 2009-05-05 22:53 |只看该作者
关于.. 请看perldoc 中 range operator 一节

你要匹配的东西是什么样子呢
拿个片断出来吧 那样才方便说

论坛徽章:
0
4 [报告]
发表于 2009-05-05 23:07 |只看该作者
--
-- Table structure for table `wp_comments`
--

DROP TABLE IF EXISTS `wp_comments`;
CREATE TABLE `wp_comments` (
  `comment_ID` bigint(20) unsigned NOT NULL auto_increment,
  `comment_post_ID` int(11) NOT NULL default '0',
  `comment_author` tinytext NOT NULL,
  `comment_author_email` varchar(100) NOT NULL default '',
  `comment_author_url` varchar(200) NOT NULL default '',
  `comment_author_IP` varchar(100) NOT NULL default '',
  `comment_date` datetime NOT NULL default '0000-00-00 00:00:00',
  `comment_date_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
  `comment_content` text NOT NULL,
  `comment_karma` int(11) NOT NULL default '0',
  `comment_approved` varchar(20) NOT NULL default '1',
  `comment_agent` varchar(255) NOT NULL default '',
  `comment_type` varchar(20) NOT NULL default '',
  `comment_parent` bigint(20) NOT NULL default '0',
  `user_id` bigint(20) NOT NULL default '0',
  PRIMARY KEY  (`comment_ID`),
  KEY `comment_approved` (`comment_approved`),
  KEY `comment_post_ID` (`comment_post_ID`),
  KEY `comment_approved_date_gmt` (`comment_approved`,`comment_date_gmt`),
  KEY `comment_date_gmt` (`comment_date_gmt`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `wp_comments`
--

--
-- Dumping data for table `wp_comments`
--

LOCK TABLES `wp_comments` WRITE;
/*!40000 ALTER TABLE `wp_comments` DISABLE KEYS */;
INSERT INTO `wp_comments` VALUES (1,1,'Mr WordPress','','http://wordpress.org/','','2009-01-23 17:22:38','2009-01-23 09:22:38
','Hi, this is a comment.<br />To delete a comment, just log in and view the post's comments. There you will have the op
tion to edit or delete them.',0,'1','','',0,0);
/*!40000 ALTER TABLE `wp_comments` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `wp_links`
--

DROP TABLE IF EXISTS `wp_links`;
省略。。。。。。


文件大体上就是这个样子。

论坛徽章:
0
5 [报告]
发表于 2009-05-06 10:02 |只看该作者
原帖由 gregorian 于 2009-5-5 22:21 发表
print if m|^-- Table structure for table `$table\b|io ..
m|/\*!40000 ALTER TABLE `$table` ENABLE KEYS \*/\;|io ;
用这个正则可以实现我要的功能,但是`好像不需要替换,;需要替换,修改了一下代码
p ...


\b在这里用得不对。 “;” 不属于word

论坛徽章:
0
6 [报告]
发表于 2009-05-06 10:47 |只看该作者
print if /^DROP TABLE IF EXISTS `$table`;/io .. /^DROP TABLE IF EXISTS `(?!$table)`;/io;
但是我这样也还是不可以,如果表的名字是wp_comments,它把wp_comments_backup wp_comments_example这些也都包括进去了

论坛徽章:
0
7 [报告]
发表于 2009-05-06 10:55 |只看该作者

  1. (?!`$table`)
复制代码

论坛徽章:
0
8 [报告]
发表于 2009-05-06 11:17 |只看该作者
谢谢 发现把后面代码改成(?!`$table`)这样还是不可以
不管是用;还是用\;都不能做到匹配,但是把;去掉就可以
顺便问问 如果我一定要匹配上;应该怎么写呢

论坛徽章:
0
9 [报告]
发表于 2009-05-06 12:31 |只看该作者

回复 #8 gregorian 的帖子


  1. (?!`$table`).*;
复制代码

估计你还是没有搞清楚(?!...)这个东西.

论坛徽章:
0
10 [报告]
发表于 2009-05-06 13:04 |只看该作者
(?!pattern) 如果正则表达式在后面不匹配 pattern ,才会开始匹配。如/foo(?!bar)/,只有当出现 foo,并且后面不出现 bar 时才开始匹配

这个不能精确匹配吗?

最后这个(?!`$table`).*;能说的清楚点吗 知道.*是匹配0或任意多个字符
但是去掉;是可以的  为什么(?!`$table`);这样就不可以
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP