1

Here is my string '2018-01-31 05:29:37 | Jan Pong Lee | [Number of contacts]:1'

I would like to remove '| Jan Pong Lee |'. The name can have 2 or 4 words, is there a way to do that?

1
  • I am still unsure about your requirements.. it is very much ambiguous. If you cant then give more input and expected output Commented Dec 19, 2018 at 6:42

2 Answers 2

3

You can use this regex to match and replace it with empty string,

\|[^|]*\|

Explanation of this regex: This regex basically captures a | followed by any character (except |) zero or more times and finally captures a | character and then stops capturing and replaces all matched characters with empty string.

Live Demo

Here is the python code for same,

import re

s = '2018-01-31 05:29:37 | Jan Pong Lee | [Number of contacts]:1'
ret = re.sub(r'\|[^|]*\|', '', s)
print (ret)

Which prints the remaining string after removal of | Jan Pong Lee |. This will work no matter whatever number of words you have inside those pipes.

2018-01-31 05:29:37  [Number of contacts]:1
Sign up to request clarification or add additional context in comments.

4 Comments

Thats very helpful!! thanks! May I know what does it mean?
@DanniPeng: Glad to help :) I have added explanation of what the regex does. Hope that helps. Let me know if you have any query further.
I've read through the demo as well, it helps me solve the biggest problem of today. Thank you again!
Pleased to help. If my answer helped you, please consider marking it as accepted answer which may benefit users looking for a similar answer.
0

Here is solution:

old_str =  '2018-01-31 05:29:37 | Jan Pong Lee | [Number of contacts]:1'
new_str = "{} | {}".format(old_str.split('|')[0], old_str.split('|')[2])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.