-2

Friends: (Revised question)

Thank you in advance for your time and attention.

import re

txt = "ear_name:zebra-wow-ear:-:4"
x = re.sub(".*:", "", txt,1)
print(x)

I was expecting the output to

zebra-wow-ear:-:4

removing

ear_name:

from

ear_name:zebra-wow-ear:-:4

but what I am getting is

4

What am I doing wrong ?

Please help.

-N

savithari
  • 107
  • 1
  • 9

1 Answers1

0

[Answer to the updated question]

You can try this:

x = re.sub(r".*?:", "", txt, 1)

which will do a non-greedy pattern match.

Alternatively, try:

x = re.sub(r"[^:]*:", "", txt, 1)

Both will replace the everything up to (and including) the first colon (:).

Note also the 4th argument is redundant in both cases, so you can also remove it and it should work.

costaparas
  • 5,047
  • 11
  • 16
  • 26