Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; "'',": 0.07; 'method,': 0.07; 'argument,': 0.09; 'issue?': 0.09; 'received:openend.se': 0.09; 'received:theraft.openend.se': 0.09; 'string)': 0.09; 'cc:addr:python-list': 0.10; 'output': 0.15; 'thu,': 0.15; 'from:addr:lac': 0.16; 'from:addr:openend.se': 0.16; 'from:name:laura creighton': 0.16; 'message-id:@fido.openend.se': 0.16; 're,': 0.16; 'received:89.233': 0.16; 'received:89.233.217': 0.16; 'received:89.233.217.133': 0.16; 'received:fido': 0.16; 'received:fido.openend.se': 0.16; 'subject:Regular': 0.16; 'string': 0.17; 'case.': 0.18; 'example.': 0.18; 'laura': 0.18; 'pfxlen:0': 0.18; "shouldn't": 0.18; 'all,': 0.20; 'skip:" 30': 0.20; 'fix': 0.21; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'do.': 0.22; 'simpler': 0.22; 'cc:no real name:2**0': 0.23; 'posted': 0.23; '2015': 0.23; 'split': 0.23; 'header:In-Reply- To:1': 0.24; 'example': 0.25; 'skip:" 20': 0.26; 'fine': 0.29; '-0700,': 0.29; 'idea,': 0.29; 'received:se': 0.29; 'fixed': 0.31; 'says': 0.32; 'skip:p 30': 0.32; 'problem': 0.33; 'skip:> 10': 0.35; 'really': 0.35; 'list': 0.35; 'but': 0.36; 'there': 0.36; 'depends': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'charset:us- ascii': 0.37; 'ok,': 0.37; 'instead': 0.38; 'skip:p 20': 0.38; 'forget': 0.60; 'real': 0.61; 'header:Message-Id:1': 0.62; 'here': 0.66; 'received:89': 0.80; '>how': 0.84 To: Palpandi cc: python-list@python.org From: Laura Creighton Subject: Re: Regular Expression In-Reply-To: Message from Palpandi of "Thu, 04 Jun 2015 06:36:29 -0700." References: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1943.1433427675.1@fido> Content-Transfer-Encoding: quoted-printable Date: Thu, 04 Jun 2015 16:21:15 +0200 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.3.9 (theraft.openend.se [89.233.217.130]); Thu, 04 Jun 2015 16:21:17 +0200 (CEST) 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: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433427684 news.xs4all.nl 2969 [2001:888:2000:d::a6]:40306 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92059 In a message of Thu, 04 Jun 2015 06:36:29 -0700, Palpandi writes: >Hi All, > >This is the case. To split "string2" from "string1_string2" I am using = >re.split('_', "string1_string2", 1) And you shouldn't be. The 3rd argument, 1 says stop after one match. >It is working fine for string "string1_string2" and output as "string2". = But actually the problem is that if a sting is "__string1_string2" and the= output is "_string1_string2". It is wrong. > >How to fix this issue? Depends on what you want. Approach #1 - just use the string method, forget re, because you do not need it. >>>> "__string1_string2".split("_") ['', '', 'string1', 'string2'] >>>> "_string1_string2__".split("_") ['', 'string1', 'string2', '', ''] Approach #2 -- use re but with a fixed string (probably a bad idea, you should be using approach 1 instead if you have a fixed string) >>>> re.split('_', "__string1_string2") ['', '', 'string1', 'string2'] >>>> re.split('_', "__string1_string2__") ['', '', 'string1', 'string2', '', ''] Approach #3 - there is a real pattern here I want to use, the example I posted to the list is a lot simpler than what I really want to do. Ok, in this case we will match 'any number of underscores' for an example. >>>> p =3D re.compile('_*') >>>> p.split("__string1_string2") ['', 'string1', 'string2'] >>>> p.split("__string1__string2__") ['', 'string1', 'string2', ''] Laura