2006年10月12日 星期四 01:02
匹配这样的数字串
1233-34-45
2343-5-56
2342-57-0
2432-6-6
一串都是数字的,分别是4个-1,2个-1,2个
r'\d{4}-\d{1,2}-\d{1,2}'
如果这样去search的话
问题是:
a = "234234-23-34324234"
b = "23 4234-23-3432 42 34"
a, b都会符合,但我只需要b,不需要a
那该怎么写正则表达式呢?
2006年10月12日 星期四 08:17
On 10/12/06, Mars Lenjoy <mars.lenjoy在gmail.com> wrote: > 匹配这样的数字串 > 1233-34-45 > 2343-5-56 > 2342-57-0 > 2432-6-6 > > 一串都是数字的,分别是4个-1,2个-1,2个 > > > > > r'\d{4}-\d{1,2}-\d{1,2}' > > 如果这样去search的话 > > 问题是: > a = "234234-23-34324234" > b = "23 4234-23-3432 42 34" > > a, b都会符合,但我只需要b,不需要a > > > 那该怎么写正则表达式呢? >>> r = re.compile(r'(\D|^)(\d{4}-\d{1,2}-\d{1,2})') >>> r.search("23 4234-23-3432 42 34").groups() (' ', '4234-23-34') (\D|^) 表示前面有一个非数字或是开头 因此你需要通过groups()[1]得到真正想要的匹配串。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2006年10月12日 星期四 08:21
http://regexlib.com/default.aspx, you can visit the website for searching what you want. Good Luck! Best Regards, Thomas 2006/10/12, Mars Lenjoy <mars.lenjoy在gmail.com>: > > Æ¥ÅäÕâÑùµÄÊý×Ö´® > 1233-34-45 > 2343-5-56 > 2342-57-0 > 2432-6-6 > > Ò»´®¶¼ÊÇÊý×ֵ쬷ֱðÊÇ4¸ö-1,2¸ö-1,2¸ö > > > > > r'\d{4}-\d{1,2}-\d{1,2}' > > Èç¹ûÕâÑùÈ¥searchµÄ»° > > ÎÊÌâÊÇ£º > a = "234234-23-34324234" > b = "23 4234-23-3432 42 34" > > a, b¶¼»á·ûºÏ£¬µ«ÎÒÖ»ÐèÒªb£¬²»ÐèÒªa > > > ÄǸÃÔõôдÕýÔò±í´ïÊ½ÄØ£¿ > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- ¡ºÃ¦Ã¦ÂµÂµ ¡ï µµÎÞΪ¡» ¡ª¡ª¡ª¡ªÒ»Ö»Ð¡ÂìÒÏ¡ª¡ª¡ª¡ª http://www.blogjava.net/qixiangnj Tel:0512-57882576 QQ:375621797 MSN:qixiangnj在gmail.com -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061012/3c4cd46e/attachment.html
2006年10月12日 星期四 13:32
On 2006年10月12日 01:02, Mars Lenjoy write:
> 匹配这样的数字串
> 1233-34-45
> 2343-5-56
> 2342-57-0
> 2432-6-6
>
> 一串都是数字的,分别是4个-1,2个-1,2个
>
>
>
>
> r'\d{4}-\d{1,2}-\d{1,2}'
>
> 如果这样去search的话
>
> 问题是:
> a = "234234-23-34324234"
> b = "23 4234-23-3432 42 34"
>
> a, b都会符合,但我只需要b,不需要a
>
>
> 那该怎么写正则表达式呢?
用re.match从目标字符串的头部开始匹配,re.search则是从任何位置开始检索-
-除非用^字符指定要匹配字串的起始位置。这在Python 的文档中有专门的描述:
http://docs.python.org/lib/matching-searching.html
这里的区别是速度。在同样的条件下,re.match会比re.search快很多。
另外,建议你给这个正则表达式的前、后增加\b,这样匹配会更准确一些。例如:
>>> re.match(r'\d{4}-\d{1,2}-\d{1,2}\b', "4234-23-34").group()
'4234-23-34'
>>> re.search(r'\b\d{4}-\d{1,2}-\d{1,2}\b', "4234-23-34").group()
'4234-23-34'
2006年10月12日 星期四 20:58
谢谢各位!
我后来用 (\s|^)\d{4}-\d{1,2}-\d{1,2}(\s|$)
2006/10/12, Xie Yanbo <xieyanbo at gmail.com>:
> On 2006年10月12日 01:02, Mars Lenjoy write:
> > 匹配这样的数字串
> > 1233-34-45
> > 2343-5-56
> > 2342-57-0
> > 2432-6-6
> >
> > 一串都是数字的,分别是4个-1,2个-1,2个
> >
> >
> >
> >
> > r'\d{4}-\d{1,2}-\d{1,2}'
> >
> > 如果这样去search的话
> >
> > 问题是:
> > a = "234234-23-34324234"
> > b = "23 4234-23-3432 42 34"
> >
> > a, b都会符合,但我只需要b,不需要a
> >
> >
> > 那该怎么写正则表达式呢?
> 用re.match从目标字符串的头部开始匹配,re.search则是从任何位置开始检索-
> -除非用^字符指定要匹配字串的起始位置。这在Python 的文档中有专门的描述:
> http://docs.python.org/lib/matching-searching.html
> 这里的区别是速度。在同样的条件下,re.match会比re.search快很多。
>
> 另外,建议你给这个正则表达式的前、后增加\b,这样匹配会更准确一些。例如:
> >>> re.match(r'\d{4}-\d{1,2}-\d{1,2}\b', "4234-23-34").group()
> '4234-23-34'
> >>> re.search(r'\b\d{4}-\d{1,2}-\d{1,2}\b', "4234-23-34").group()
> '4234-23-34'
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
Zeuux © 2025
京ICP备05028076号