0

I have a string in which date and time change according to the report.

String = "Engine NOrc04/14/20 11:24XX5555-May-2021" 

I want to remove "04/14/20 11:24" because it changes every time when I generate report.

output = "Engine NOrcXX5555-May-2021"
3
  • You should provide more how you construct the string and internal format of the string? Commented Apr 19, 2020 at 14:25
  • Have you tried any efforts to solve this issue Commented Apr 19, 2020 at 14:28
  • Is this answer to your questionclick here Commented Apr 19, 2020 at 14:31

2 Answers 2

1

Slicing

If the format of the string doesn't change you can slice the string as follows:

string = "Engine NOrc04/14/20 11:24XX5555-May-2021"
new_string = string[:11] + string[25:]
print(new_string)

string = "Engine NOrc04/14/20 11:24XX5555-May-2021 report contains below information..."
new_string = string[:11] + string[25:]
print(new_string)

Output

Engine NOrcXX5555-May-2021
Engine NOrcXX5555-May-2021 report contains below information...

Using regex

If the time appears in different parts of the string, but the format of the time doesn't change, you can use re.sub() to replace the time string:

import re

string = 'Engine NOrc04/14/20 11:24XX5555-May-2021 report contains below information...'
new_string = re.sub('\d\d/\d\d/\d\d \d\d:\d\d', '', string)
print(new_string)

string = 'SOME OTHER RANDOM STRING 04/14/20 11:24 THIS IS A DIFFERENT STRING'
new_string = re.sub('\d\d/\d\d/\d\d \d\d:\d\d', '', string)
print(new_string)

Output:

Engine NOrcXX5555-May-2021 report contains below information...
SOME OTHER RANDOM STRING  THIS IS A DIFFERENT STRING
Sign up to request clarification or add additional context in comments.

13 Comments

actually string length is not fixed, it is getting changed but the part which i want to remove will always start from NOrc and ends with time(time changes with every report)- for example - String = "Report generated by:vikas report name:oldreport report type and generated on:Engine NOrc04/14/20 11:24XX5555-May-2021 report contains below information..." in above scenario i do not know the index of "04/14/20 11:24" please help Thanks
@VinodKumar updated my answer, instead of counting from the end, just skip 14 chars since that's how long the substring for the time is
@GrandPhuba You've just copied my regex solution into your answer! Counting characters is an extremely brittle way of solving this.
@ScoJo I would concede that the code is the same and you suggested it first, but do you really think one would blindly copy code as simple as that, let alone provide it as part of an answer without testing it. As for the brittle comment, that is beside the point, unless OP provides more context why it is unfit, the answer works, and is the sole reason why answer tries explain when to use which.
|
0

Use regex to find the date and replace with an empty string.

import re

string = "Engine NOrc04/14/20 11:24XX5555-May-2021"
result = re.sub(r"\d\d/\d\d/\d\d \d\d:\d\d", "", string)
print(result)

output

'Engine NOrcXX5555-May-2021'

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.