Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase Newsgroups: comp.lang.python Subject: Re: Regular expressions Date: Tue, 3 Nov 2015 05:50:18 -0600 Lines: 38 Message-ID: References: <662g3blobme52hfoududj27err185v2npm@4ax.com> <20151102204237.6a78abdf@bigbox.christie.dr> <56382F33.8050905@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de e2J9odb0yEOHcJtbeceyOAPApTtpzQJTKtEBwW9gIw3A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'else:': 0.03; 'trailing': 0.07; 'def': 0.13; '-tkc': 0.16; 'file?': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterating': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Regular': 0.16; 'subject:expressions': 0.16; 'wrote:': 0.16; 'string': 0.17; ';-)': 0.18; '>>>': 0.20; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'function': 0.28; 'regular': 0.29; 'too.': 0.30; 'code': 0.30; 'usually': 0.33; 'false': 0.35; 'according': 0.36; 'but': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'two': 0.37; 'requirement': 0.37; 'charset:us-ascii': 0.37; 'end': 0.39; 'why': 0.39; 'rather': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'better.': 0.66; 'asterisk': 0.84; 'catch:': 0.84; "op's": 0.84; 'otten': 0.84; 'received:23': 0.84; 'too:': 0.84 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1446551498389:2269436066 X-MC-Ingress-Time: 1446551498389 In-Reply-To: X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-AuthUser: tim@thechases.com 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:98147 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). -tkc