Regular Expression

Tim Chase python.list at tim.thechases.com
Thu Jun 4 09:48:55 EDT 2015


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






More information about the Python-list mailing list