免费注册 查看新帖 |

Chinaunix

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

已经解决.结贴 脚本 Perl - 正则-1 匹配空行 (避免行终止符后面的插值) [复制链接]

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-10-09 13:08 |只看该作者 |倒序浏览
本帖最后由 sunzhiguolu 于 2015-10-09 16:10 编辑

我的代码文件如下:

  1. #!/usr/bin/perl
  2. use 5.010;
  3. use strict;
  4. use warnings;

  5. my ($str_original, $str) = ("\n A\n \n B \n\n");
  6. say "Display Original String :\"$str_original\"";
  7. $str = $str_original;
  8. $str =~ s/^[ \t]*$\\n//mg;
  9. #$str =~ s/^[ \t]*(?:$)\n//mg;
  10. #$str =~ s/^[ \t]*$(?)\n//mg;
  11. say "Display Replace Result :\"$str\"";
复制代码
输出结果如下:
  1. Display Original String :"
  2. A

  3. B

  4. "
  5. Display Replace Result :" A
  6. B
  7. "
复制代码
在这里我有两点不明白, 希望得到大家的指点及帮助. 在此先谢过大家了...
$str =~ s/^[ \t]*$\\n//mg;

1.> 代码中第 9 行的 \\ 字符, 如果改成单个的话, 则会报告如下错误:
      "Use of uninitialized value $\ in regexp compilation ...", 难道 \\ 消除了对 $\ 内置变量的歧义?
2.> 如果希望使用其他的表达方式, 我该如何书写元字符 $ 及后面的换行符 即 \n 以避免歧义.

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
2 [报告]
发表于 2015-10-09 13:30 |只看该作者
本帖最后由 zhlong8 于 2015-10-09 13:31 编辑

perldiag

Possible unintended interpolation of $\ in regex

(W ambiguous) You said something like m/$\/ in a regex. The regex m/foo$\s+bar/m translates to: match the word 'foo', the output record separator (see $\ in perlvar) and the letter 's' (one time or more) followed by the word 'bar'.

If this is what you intended then you can silence the warning by using m/${\}/ (for example: m/foo${\}s+bar/).

If instead you intended to match the word 'foo' at the end of the line followed by whitespace and the word 'bar' on the next line then you can use m/$(?)\/ (for example: m/foo$(?)\s+bar/).

直接加不捕获的括号  (?:$)\n 也行

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
3 [报告]
发表于 2015-10-09 13:37 |只看该作者
回复 2# zhlong8
谢谢您的指点, 我懂了...

   

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
4 [报告]
发表于 2015-10-09 13:43 |只看该作者
回复 3# sunzhiguolu


    那你教教我 (?) 是个什么东西

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
5 [报告]
发表于 2015-10-09 13:49 |只看该作者
本帖最后由 sunzhiguolu 于 2015-10-09 13:51 编辑

我恐怕不能完成教教您的任务, 但是我可以把我的理解向您说一下有什么不对的地方还请您指点:
(?:) #这个是非捕获组, 将元字符 $ 放入其中的含义就是为了与其后面的换行符 即 \n 避免歧义. (不知道这样理解对吗?)

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
6 [报告]
发表于 2015-10-09 14:27 |只看该作者
本帖最后由 sunzhiguolu 于 2015-10-09 15:03 编辑

回复 4# zhlong8
If instead you intended to match the word 'foo' at the end of the line followed by whitespace and the word 'bar' on the next line then you can use m/$(?)\/ (for example: m/foo$(?)\s+bar/).

大神您好, 我理解错了.
这个符号我在方才看您给出的帮助的时候才注意到我答非所问了. 让您见笑了.

您能否说一下这是个什么符号? 并且的确在表达式中用了这个符号后也能避免在元字符 $ 与 换行符 即 \n 之间的歧义.
(我在网上找了一下关于 "Possible unintended interpolation of $\ in regex" 这个主题的文章, 但是除了像您给出的帮助类似的内容之外, 什么都没有找到.)

   

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
7 [报告]
发表于 2015-10-09 14:34 |只看该作者
本帖最后由 sunzhiguolu 于 2015-10-09 14:36 编辑

    如果大家知道 (?) 是个什么符号, 表示什么含义还请您给予指点.  (?)

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
8 [报告]
发表于 2015-10-09 15:22 |只看该作者
回复 6# sunzhiguolu

就是个空的这东西。 Perl 文档很完备,关于语言本身有 perldoc 不需要搜索引擎,我贴的都是 perldoc 里的内容。

http://perldoc.perl.org/perlre.html#Extended-Patterns

(?adlupimsx-imsx)
(?^alupimsx)

One or more embedded pattern-match modifiers, to be turned on (or turned off, if preceded by - ) for the remainder of the pattern or the remainder of the enclosing pattern group (if any).

This is particularly useful for dynamic patterns, such as those read in from a configuration file, taken from an argument, or specified in a table somewhere. Consider the case where some patterns want to be case-sensitive and some do not: The case-insensitive ones merely need to include (?i) at the front of the pattern. For example:

        $pattern = "foobar";
        if ( /$pattern/i ) { }
        # more flexible:
        $pattern = "(?i)foobar";
        if ( /$pattern/ ) { }

These modifiers are restored at the end of the enclosing group. For example,

        ( (?i) blah ) \s+ \g1

will match blah in any case, some spaces, and an exact (including the case!) repetition of the previous word, assuming the /x modifier, and no /i modifier outside this group.

These modifiers do not carry over into named subpatterns called in the enclosing group. In other words, a pattern such as ((?i)(?&NAME)) does not change the case-sensitivity of the "NAME" pattern.

Any of these modifiers can be set to apply globally to all regular expressions compiled within the scope of a use re . See '/flags' mode in re.

Starting in Perl 5.14, a "^" (caret or circumflex accent) immediately after the "?" is a shorthand equivalent to d-imsx . Flags (except "d" ) may follow the caret to override it. But a minus sign is not legal with it.

Note that the a , d , l , p , and u modifiers are special in that they can only be enabled, not disabled, and the a , d , l , and u modifiers are mutually exclusive: specifying one de-specifies the others, and a maximum of one (or two a 's) may appear in the construct. Thus, for example, (?-p) will warn when compiled under use warnings ; (?-d:...) and (?dl:...) are fatal errors.

Note also that the p modifier is special in that its presence anywhere in a pattern has a global effect.

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
9 [报告]
发表于 2015-10-09 15:26 |只看该作者
本帖最后由 sunzhiguolu 于 2015-10-09 16:09 编辑

回复 4# zhlong8
    您好, 方才我进行了一些测试. 发现 (?) 与正则表达式没有什么关系. 是 Perl 语言自身的符号.
(作用是避免行锚定符号 "$" 后面紧跟 "\" 发生歧义, 也不知道我的理解是否正确, 具体该符号的名称及功能希望您给予帮助及支持...)

   

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
10 [报告]
发表于 2015-10-09 15:44 |只看该作者
本帖最后由 sunzhiguolu 于 2015-10-09 15:50 编辑

回复 8# zhlong8
Starting in Perl 5.14, a "^" (caret or circumflex accent) immediately after the "?" is a shorthand equivalent to d-imsx . Flags (except "d" ) may follow the caret to override it. But a minus sign is not legal with it.

您好, 您给出的这句帮助用翻译工具翻译出来的结果看不懂. 您能否用中文解释一下.

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP