Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Regular expressions Date: Tue, 03 Nov 2015 15:00:48 +0100 Organization: None Lines: 41 Message-ID: References: <662g3blobme52hfoududj27err185v2npm@4ax.com> <20151102204237.6a78abdf@bigbox.christie.dr> <56382F33.8050905@gmail.com> <20151103055018.535e3e42@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de vatS/ODh8M8kvIgQHuyUsQncbV1o6nVSymO5RuMcF04Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'newline': 0.07; 'trailing': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'satisfy': 0.09; 'def': 0.13; 'file?': 0.16; 'iterating': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Regular': 0.16; 'subject:expressions': 0.16; 'wrote:': 0.16; 'string': 0.17; ';-)': 0.18; '>>>': 0.20; 'defined': 0.23; 'tim': 0.24; 'header :User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; 'regular': 0.29; 'chase': 0.29; 'too.': 0.30; 'code': 0.30; 'usually': 0.33; 'requirement.': 0.33; 'false': 0.35; 'according': 0.36; 'but': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'requirement': 0.37; 'end': 0.39; 'why': 0.39; 'rather': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'better.': 0.66; 'asterisk': 0.84; 'catch:': 0.84; "op's": 0.84; 'otten': 0.84; 'too:': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9867.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98154 Tim Chase wrote: > On 2015-11-03 10:25, Peter Otten wrote: >> >>> How do I make a regular expression that returns true if the end >> >>> of the line is an asterisk >> >> >> >> Why use a regular expression? >> >> >> >> if line[-1] == '*': >> >> yep(line) >> >> else: >> >> nope(line) >> >> Incidentally the code example has two "problems", too. >> >> - What about the empty string? > > Good catch: .endswith() works better. > >> - What about lines with a trailing "\n", i. e. as they are usually >> delivered when iterating over a file? > > Then your string *doesn't* end with a "*", but rather with a > newline. ;-) > > Though according to the OP's specs, the following function would work > too: > > def ends_in_asterisk(s): > return True > > It *does* return True if the line ends in an asterisk (no requirement > to make the function return False under any other conditions). If a "line" is defined as a string that ends with a newline def ends_in_asterisk(line): return False would also satisfy the requirement. Lies, damned lies, and specs ;)