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 10:25:26 +0100 Organization: None Lines: 83 Message-ID: References: <662g3blobme52hfoududj27err185v2npm@4ax.com> <20151102204237.6a78abdf@bigbox.christie.dr> <56382F33.8050905@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de xtTDwfND7fI4Jpwf3sNR+AP/VKC3gSSmQZ7IDvan9X5g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'python3': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'trailing': 0.07; 'indeed,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'substring': 0.09; 'template': 0.11; 'def': 0.13; 'options.': 0.15; '"*"': 0.16; "'*'": 0.16; 'data)': 0.16; 'file?': 0.16; 'indexerror:': 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; 'row': 0.16; 'subject:Regular': 0.16; 'subject:expressions': 0.16; 'wrote:': 0.16; 'char': 0.18; 'skip:l 30': 0.18; 'try:': 0.18; '>>>': 0.20; 'people,': 0.20; '"",': 0.22; 'import': 0.24; 'skip:b 30': 0.24; 'tim': 0.24; 'header': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:" 20': 0.26; 'yield': 0.27; "skip:' 10": 0.28; 'regular': 0.29; 'cat': 0.29; 'chase': 0.29; 'comparison': 0.29; 'too.': 0.30; 'code': 0.30; 'skip:[ 10': 0.31; 'included': 0.32; 'michael': 0.33; 'usually': 0.33; "i'll": 0.33; 'except': 0.34; 'false': 0.35; 'quite': 0.35; 'sometimes': 0.35; 'lines': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'end': 0.39; 'why': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'your': 0.60; 'sample': 0.63; 'results': 0.66; 'obvious': 0.76; 'asterisk': 0.84; 'seymore4head': 0.84; '"one': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9f29.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:98143 Michael Torrie wrote: > On 11/02/2015 07:42 PM, Tim Chase wrote: >> On 2015-11-02 20:09, Seymore4Head 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) > > Indeed, sometimes Jamie Zawinski's is often quite appropriate: > > Some people, when confronted with a problem, think "I know, I'll use > regular expressions." Now they have two problems. Incidentally the code example has two "problems", too. - What about the empty string? - What about lines with a trailing "\n", i. e. as they are usually delivered when iterating over a file? Below is a comparison of some of your options. The "one obvious way" line.rstrip("\n").endswith("*") is not included ;) $ cat starry_table.py import re def show_table(data, header): rows = [header] rows.extend([str(c) for c in row] for row in data) widths = [max(len(row[i]) for row in rows) for i in range(len(header))] template = " ".join("{:%d}" % w for w in widths) for row in rows: print(template.format(*row)) def compare(sample_lines): for line in sample_lines: got_re = bool(re.compile("\*$").search(line)) got_re_M = bool(re.compile("\*$", re.M).search(line)) got_endswith = line.endswith("*") got_endswith2 = line.endswith(("*", "*\n")) got_substring = line[-1:] == "*" try: got_char = line[-1] == "*" except IndexError: got_char = "#exception" results = ( got_re, got_re_M, got_endswith, got_endswith2, got_substring, got_char) yield ( ["", "X"][len(set(results)) > 1], repr(line)) + results SAMPLE = ["", "\n", "foo\n", "*\n", "*", "foo*", "foo*\n", "foo*\nbar"] HEADER = [ "", "line", "regex", "re.M", "endswith", 'endswith(("*", "*\\n"))', "substring", "char"] if __name__ == "__main__": show_table(compare(SAMPLE), HEADER) $ python3 starry_table.py line regex re.M endswith endswith(("*", "*\n")) substring char X '' False False False False False #exception '\n' False False False False False False 'foo\n' False False False False False False X '*\n' True True False True False False '*' True True True True True True 'foo*' True True True True True True X 'foo*\n' True True False True False False X 'foo*\nbar' False True False False False False