Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'patterns': 0.04; 'expressions': 0.07; 'parsing': 0.07; 'repeated': 0.07; 'booth': 0.09; 'finite': 0.09; 'sentence': 0.09; ':-)': 0.13; 'language': 0.14; '"hey': 0.16; '(low': 0.16; '*you*': 0.16; 'driscoll': 0.16; 'element.': 0.16; 'excludes': 0.16; 'expressions)': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'regex,': 0.16; 'subject:expression': 0.16; 'subject:regular': 0.16; 'wrote:': 0.17; 'element': 0.17; '(in': 0.18; 'followed': 0.20; 'parse': 0.22; 'recognize': 0.22; 'simpler': 0.22; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'converting': 0.27; 'regular': 0.27; "doesn't": 0.28; 'received:192.168.1.3': 0.29; 'way?': 0.29; "i'm": 0.29; '(including': 0.30; 'knows': 0.30; 'e.g.': 0.30; '(and': 0.32; 'says': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'so,': 0.35; 'really': 0.36; "didn't": 0.36; 'should': 0.36; 'problems': 0.36; 'two': 0.37; 'why': 0.37; 'mean': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'decision': 0.60; 'most': 0.61; 'first': 0.61; 'story': 0.61; 'is.': 0.62; 'repeat': 0.62; 'techniques': 0.63; 'different': 0.63; 'real-world': 0.65; 'header:Reply-To:1': 0.68; 'fact,': 0.69; 'reply-to:no real name:2**0': 0.72; 'designers': 0.75; 'catastrophic': 0.84; 'reply- to:addr:python.org': 0.84; 'reveal': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=OqzNOlDt c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=AAvI7MrX_rgA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=hmEw4vkPLQIA:10 a=V7NpVvWzO7N1l_BLxn0A:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Fri, 05 Oct 2012 17:07:47 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120907 Thunderbird/15.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: + in regular expression References: <506EFC44.40508@cs.wisc.edu> In-Reply-To: <506EFC44.40508@cs.wisc.edu> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349453267 news.xs4all.nl 6914 [2001:888:2000:d::a6]:43477 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30825 On 2012-10-05 16:27, Evan Driscoll wrote: > On 10/05/2012 04:23 AM, Duncan Booth wrote: >> A regular expression element may be followed by a quantifier. >> Quantifiers are '*', '+', '?', '{n}', '{n,m}' (and lazy quantifiers >> '*?', '+?', '{n,m}?'). There's nothing in the regex language which says >> you can follow an element with two quantifiers. > In fact, *you* did -- the first sentence of that paragraph! :-) > > \s is a regex, so you can follow it with a quantifier and get \s{6}. > That's also a regex, so you should be able to follow it with a quantifier. > > I can understand that you can create a grammar that excludes it. I'm > actually really interested to know if anyone knows whether this was a > deliberate decision and, if so, what the reason is. (And if not -- > should it be considered a (low priority) bug?) > > Was it because such patterns often reveal a mistake? Because "\s{6}+" > has other meanings in different regex syntaxes and the designers didn't > want confusion? Because it was simpler to parse that way? Because the > "hey you recognize regular expressions by converting it to a finite > automaton" story is a lie in most real-world regex implementations (in > part because they're not actually regular expressions) and repeated > quantifiers cause problems with the parsing techniques that actually get > used? > You rarely want to repeat a repeated element. It can also result in catastrophic backtracking unless you're _very_ careful. In many other regex implementations (including mine), "*+", "*+" and "?+" are possessive quantifiers, much as "??", "*?" and "??" are lazy quantifiers. You could, of course, ask why adding "?" after a quantifier doesn't make it optional, e.g. why r"\s{6}?" doesn't mean the same as r"(?:\s{6})?", or why r"\s{0,6}?" doesn't mean the same as r"(?:\s{0,6})?".