免费注册 查看新帖 |

Chinaunix

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

[其他] 正则表达式 [复制链接]

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-10-31 15:50 |只看该作者 |倒序浏览

man regex

       Within  a  bracket  expression, a collating element (a character, a multi-character sequence that collates as if it were a single character, or a collating-sequence name
       for either) enclosed in "[." and ".]" stands for the sequence of characters of that collating element.  The sequence is a single  element  of  the  bracket  expression’s
       list.  A bracket expression containing a multi-character collating element can thus match more than one character
, for example, if the collating sequence includes a "ch"
       collating element, then the RE "[[.ch.]]*c" matches the first five characters of "chchcc".

       Within a bracket expression, a collating element enclosed in "[=" and "=]" is an equivalence class, standing for the sequences of characters of  all  collating  elements
       equivalent  to  that  one,  including itself.  (If there are no other equivalent collating elements, the treatment is as if the enclosing delimiters were "[." and ".]".)
       For example, if o and ^ are the members of an equivalence class, then "[[=o=]]", "[[=^=]]", and "[o^]" are all synonymous.  An equivalence class may not(!)  be  an  end-
       point of a range.

"collating element" 如果看中文的man翻译就是"归并元素"

一直没搞懂这个[..]是怎么用的?

试了一下egrep、grep、sed、awk貌似都不支持。

拿egrep 来说:

regex.log:
0000ae0000ll000
0000ax0000ll000

egrep "[[.ae.]]" regex.log
egrep: Invalid collation character

egrep "[[.a.]]" regex.log
0000ae0000ll000
0000ax0000ll000

如果不支持多个字符的形式(即[.多个字符.]),那粗体部分描述的意思是什么?貌似就失去意义了。

ps:下面那段的"等价类"就更难理解了,更晦涩。

论坛徽章:
5
双鱼座
日期:2013-11-26 17:56:26狮子座
日期:2013-11-29 15:41:32处女座
日期:2014-02-21 11:59:07技术图书徽章
日期:2014-03-06 15:33:53技术图书徽章
日期:2014-03-06 15:39:30
2 [报告]
发表于 2013-10-31 16:38 |只看该作者
你的glibc是什么版本的..2.18之前好像有这个问题的

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
3 [报告]
发表于 2013-10-31 17:38 |只看该作者
本帖最后由 chenzhanyiczy 于 2013-10-31 17:39 编辑

都是2.18前的。。。

GNU C Library stable release version 2.12, by Roland McGrath et al.

GNU C Library stable release version 2.5, by Roland McGrath et al.

请问: 哪里有解析,2.18前存在这个问题?

另外:会不会跟区域有关? LANG、LC_ALL之类的

论坛徽章:
5
双鱼座
日期:2013-11-26 17:56:26狮子座
日期:2013-11-29 15:41:32处女座
日期:2014-02-21 11:59:07技术图书徽章
日期:2014-03-06 15:33:53技术图书徽章
日期:2014-03-06 15:39:30
4 [报告]
发表于 2013-10-31 18:02 |只看该作者
按照这个帖子所述,应该是glibc的一个bug,然后据说已经在2.18解决了

https://sourceware.org/bugzilla/show_bug.cgi?id=11561

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
5 [报告]
发表于 2013-10-31 21:37 |只看该作者
回复 4# bottles


这个是说,[..]里面需要用到"归并标识"(collating symbols),而不是用“归并元素”(collating element)。
因为用"归并元素"会有不好的地方。
这是glibc需要改进的地方。 这个说的跟我的疑问不是一个意思。

其实[..]是跟区域、语言环境有关的,也就是说跟Locale有关。

单看上面的man那段,给人很大误解,以为是像这样的正则:

grep "[[.ae.]]" regex.log

是把ae当作一个整体字符串来匹配。事实上,确实是这样。但是有前提的,那就是需要Locale做类似如下的"映射"

collating-symbol  <ae>
collating-element <a-e> from "<U007A><U0073>"

只是因为该死的man写的如此简洁,一句"废话"都没有。给人的感觉就是正则已经实现了该功能,对前提提的太少,也没有提到具体的“归并”含义

如果没有这个前提,regex能实现对所有字符都实现该功能,那好处还是有很多的。

比如要匹配:
111aeaeae111
xxx000xxxxx

egrep "[0[.ae.]]{3,}" ,多简洁。

论坛徽章:
5
双鱼座
日期:2013-11-26 17:56:26狮子座
日期:2013-11-29 15:41:32处女座
日期:2014-02-21 11:59:07技术图书徽章
日期:2014-03-06 15:33:53技术图书徽章
日期:2014-03-06 15:39:30
6 [报告]
发表于 2013-11-01 02:20 |只看该作者
回复 5# chenzhanyiczy


    我又看了一遍那个帖子,我怎么老觉得他们讲的就是你问的这个意思呢?我现在把他们的话一段段翻译下。
之前你问的是不知道[..]怎么使用,因为再调用了[[.ae]]之后还是返回错误。

然后他们的帖子上lz提出了一个问题,他说:

In the glibc locale definitions, collating elements have a hyphenated name:
在glibc定义里面,collating element这个定义是有连接符'-'的。

    collating-symbol  <zs>
这种没有链接符的形式
    collating-element <z-s> from "<U007A><U0073>"
这种有链接符的形式

and the hyphenated name have to be used in regular expression for [[. .]] to
work properly:
然后有链接符的名字的正则表达式工作很正常

    $ echo '*ch*' | LC_COLLATE=cs_CZ.UTF-8 sed 's/[[.c-h.]]//'
    **
这个工作很正常,sed确实把ch去掉了
    $ echo 'ch' | LC_COLLATE=cs_CZ.UTF-8 sed 's/[[.ch.]]//'
    sed: -e expression #1, char 12: Invalid collation character
没有链接符号的就工作不正常。

However, POSIX 1.2008 says:

        A collating symbol is a collating element enclosed within
        bracket-period ( "[." and ".]" ) delimiters. Collating
        elements are defined as described in Collation Order .
        Conforming applications shall represent multi-character
        collating elements as collating symbols when it is
        necessary to distinguish them from a list of the
        individual characters that make up the multi-character
        collating element. For example, if the string "ch" is a
        collating element defined using the line:

        collating-element <ch-digraph> from "<c><h>"

        in the locale definition, the expression "[[.ch.]]" shall
        be treated as an RE containing the collating symbol 'ch',
        while "[ch]" shall be treated as an RE matching 'c' or
        'h' . Collating symbols are recognized only inside
        bracket expressions. If the string is not a collating
        element in the current locale, the expression is invalid.

这段话解释了collating symbols和collating element的区别,一个collating symbol 等于一个collating element用[.和.]这样的分割符围起来的符号,恩。。感觉这两个没啥大区别阿?


POSIX especially mentions [[.ch.]] in the example instead of [[.ch-digraph.]] so
this is a bug in glibc.  It shouldn't be hard to fix it in regcomp.
posix只在样例当中提到了[[.ch.]]的用法,而没有提到连字符'-'的用法,而且他在测试当中发现没有连字符的返回结果不正常,所以他认为这是一个bug。

然后一楼的人回复:

> POSIX especially mentions [[.ch.]] in the example instead of [[.ch-digraph.]]
> so this is a bug in glibc.  It shouldn't be hard to fix it in regcomp.

The easiest fix would be, in my opnion, to rename all the
collation-element names for digraphs from their hyphenated
form to the non-hyphenated form.  But a few users may have
gotten used to using the hyphented forms, working around
this bug in glibc.  They would be pissed.  So for quite a
while both forms will have to recognized.  Attached patch
is an attempt to do this -- when the user specifies [.xx.],
it will first try to look up "xx" in the table of collation
elements, and when that fails, it will look up "x-x".  Is
this what you had in mind, Paolo?

最简单的就是让代码里本来实现连字符格式的变成非连字符格式的。但是由于小部分用户可能习惯用连字符格式的了,要是把这些改了他们就遭殃了。所以我想让2种格式的都能兼容应该更好。我的附件的patch就是这么做的blabla...

然后他附件的代码的改动:

  int32_t elem, idx;
2848                      elem = seek_collating_symbol_entry (br_elem->opr.name,
2849                                                          sym_name_len);
2850                      if (symb_table[2 * elem] == 0 && sym_name_len == 2)
2851                        {
2852                          br_elem->opr.name[3] = '\0';
2853                          br_elem->opr.name[2] = br_elem->opr.name[1];
2854                          br_elem->opr.name[1] = '-';
2855                          elem = seek_collating_symbol_entry (br_elem->opr.name, 3);
2856                        }
2850                      if (symb_table[2 * elem] != 0)        2857                      if (symb_table[2 * elem] != 0)
2851                        {        2858                        {
2852                          /* We found the entry.  */        2859                          /* We found the entry.  */



2850 到2856是他新增的行数,这里很清楚的表明了他增加了不带连字符格式的处理。


(Untested patch.  I'm just majorly annoyed that collation elements
don't work as one would expect from the documenmtation.)

最后一层楼的人说:

Fixed in 2.18.

表明这个问题在2.18版本当中修复了。

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
7 [报告]
发表于 2013-11-01 10:06 |只看该作者
本帖最后由 chenzhanyiczy 于 2013-11-01 10:06 编辑

回复 6# bottles


   
那个帖子主要讨论的是 collating-symbol  和 collating-element 在[..]中的不同表现,collating-symbol 在[..]的缺陷 。也就是说他们讨论的是用法细节。

而我最初疑问是,[..]的用途。 也就是5楼的解析。

不过这个帖子还给我一些暗示--[..]这功能跟区域、语言有关
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP