Chinaunix

标题: 已经解决.结贴 正则 - 这个字符组是啥意思 [打印本页]

作者: sunzhiguolu    时间: 2016-01-23 21:14
标题: 已经解决.结贴 正则 - 这个字符组是啥意思
本帖最后由 sunzhiguolu 于 2016-01-25 00:54 编辑
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;

  4. my $sStr = "[]";
  5. print "Matched!\n" if ($sStr =~ m/[][]/);
复制代码
请问大家上面正则 "[][]" 第一个左方括号遇到第一个右方括号是否结束了? 当我用表达式 "[]" 的时候则会报错.
能给解释一下吗? 谢谢大家...
作者: sunzhiguolu    时间: 2016-01-23 21:20
本帖最后由 sunzhiguolu 于 2016-01-23 21:40 编辑

另外, 还有:
1.>
  1. print "Matched!\n" if ($sStr =~ m/[]]/);
复制代码
2.>
  1. print "Matched!\n" if ($sStr =~ m/[[]]/);
复制代码
左, 右 方括号在什么位置才作为字符组的元字符处理.
作者: zhlong8    时间: 2016-01-24 13:28
perlre 中有详细的正则表达式语法,[] 是元字符永远需要转义
作者: sunzhiguolu    时间: 2016-01-24 14:51
本帖最后由 sunzhiguolu 于 2016-01-24 14:55 编辑

回复 3# zhlong8
感觉在上面没有进行转义但是却与转义后的效果相似. 谢谢大神指点, 我再看看帮助文档.

   
作者: zhlong8    时间: 2016-01-24 20:59
回复 4# sunzhiguolu


perlrecharclass 全转义就好了,记这些个特殊情况有什么用。感觉模棱两可的东西尽量简化问题,该转义转义,改加括号加括号,没必要挑战语法的极限。

   
Special Characters Inside a Bracketed Character Class

Most characters that are meta characters in regular expressions (that is, characters that carry a special meaning like ., * , or () lose their special meaning and can be used inside a character class without the need to escape them. For instance, [()] matches either an opening parenthesis, or a closing parenthesis, and the parens inside the character class don't group or capture.

Characters that may carry a special meaning inside a character class are: \ , ^, - , [ and ], and are discussed below. They can be escaped with a backslash, although this is sometimes not needed, in which case the backslash may be omitted.

The sequence \b is special inside a bracketed character class. While outside the character class, \b is an assertion indicating a point that does not have either two word characters or two non-word characters on either side, inside a bracketed character class, \b matches a backspace character.

The sequences \a , \c , \e , \f , \n , \N{NAME}, \N{U+hex char}, \r , \t , and \x are also special and have the same meanings as they do outside a bracketed character class.

Also, a backslash followed by two or three octal digits is considered an octal number.

A [ is not special inside a character class, unless it's the start of a POSIX character class (see POSIX Character Classes below). It normally does not need escaping.

A ] is normally either the end of a POSIX character class (see POSIX Character Classes below), or it signals the end of the bracketed character class. If you want to include a ] in the set of characters, you must generally escape it.

However, if the ] is the first (or the second if the first character is a caret) character of a bracketed character class, it does not denote the end of the class (as you cannot have an empty class) and is considered part of the set of characters that can be matched without escaping.

Examples:

     "+"   =~ /[+?*]/     #  Match, "+" in a character class is not special.
     "\cH" =~ /[\b]/      #  Match, \b inside in a character class
                          #  is equivalent to a backspace.
     "]"   =~ /[][]/      #  Match, as the character class contains
                          #  both [ and ].
     "[]"  =~ /[[]]/      #  Match, the pattern contains a character class
                          #  containing just [, and the character class is
                          #  followed by a ].

作者: sunzhiguolu    时间: 2016-01-25 00:52
回复 5# zhlong8
非常感谢您的指点, 明白了.

   
作者: zhlong8    时间: 2016-01-25 11:14
回复 6# sunzhiguolu


    比如引用这最后一段,明明[]是元字符但因为觉得你不会要一个空的字符集,所以就允许 []] 等于 [\]] 。正则表达式都够繁杂了,总有些蠢蛋觉得还不够,稍有点逻辑空间就加上这种所谓 “智能” “优化”,Perl 里很多地方都能做类型推断你倒是给它加上JIT 啊。
作者: sunzhiguolu    时间: 2016-01-25 13:27
回复 7# zhlong8
Perl 里很多地方都能做 类型推断 你倒是给它加上 JIT 啊。

大神您所说的类型推断以及 JIT 我查了下, 好像没有找到相关的内容. 您能提供下学习线索吗? 谢谢您...

   
作者: sunzhiguolu    时间: 2016-01-25 20:58
回复 7# zhlong8
非常感谢您的帮助, 谢谢...

   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2