Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92054

Re: Regular Expression

Date 2015-06-04 08:48 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: Regular Expression
References <c85bc324-37fe-469e-b3b1-b1d4e51bf7d8@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.161.1433426138.13271.python-list@python.org> (permalink)

Show all headers | View raw


On 2015-06-04 06:36, Palpandi wrote:
> This is the case. To split "string2" from "string1_string2" I am
> using re.split('_', "string1_string2", 1)[1].
> 
> 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.

Why use regular expressions to split a string on a constant?

Try

  for input in [
      "string1_string2",
      "__string1_string2",
      ]:
    value = input.rsplit('_', 1)[-1]
    assert value == "string2"

-tkc


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Regular Expression Palpandi <palpandi111@gmail.com> - 2015-06-04 06:36 -0700
  Re: Regular Expression Larry Martell <larry.martell@gmail.com> - 2015-06-04 09:43 -0400
  Re: Regular Expression Steven D'Aprano <steve@pearwood.info> - 2015-06-04 23:54 +1000
  Re: Regular Expression Tim Chase <python.list@tim.thechases.com> - 2015-06-04 08:48 -0500
  Re: Regular Expression Peter Otten <__peter__@web.de> - 2015-06-04 16:00 +0200
  Re: Regular Expression Laura Creighton <lac@openend.se> - 2015-06-04 16:21 +0200

csiph-web