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"
some_programmer
  • 3,268
  • 4
  • 24
  • 59

2 Answers2

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
Community
  • 1
  • 1
Victor
  • 2,864
  • 1
  • 12
  • 20
  • 1
    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 – Vinod Kumar Apr 19 '20 at 14:40
  • @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 – Victor Apr 19 '20 at 14:46
  • @GrandPhuba You've just copied my regex solution into your answer! Counting characters is an extremely brittle way of solving this. – ScoJo Apr 19 '20 at 14:59
  • @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. – Victor Apr 19 '20 at 15:07
  • https://stackoverflow.com/questions/11475885/python-replace-regex https://stackoverflow.com/questions/5658369/how-to-input-a-regex-in-string-replace – Victor Apr 19 '20 at 15:15
  • @GrandPhuba Thanks this worked I have another string which contains - Date Prepared : 14-Apr-2020 12:06 and Item added Date 04-May-2021 07:00 in above scenarios i want to remove only "14-Apr-2020 12:06" and this date and time is not static but format is static Please help me solve this also Thank you – Vinod Kumar Apr 19 '20 at 15:29
  • @Vinod Kumar You can use `re.sub('\d\d-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d\d\d\d \d\d:\d\d', '', "Date Prepared : 14-Apr-2020 12:06")` and it will return `"Date Prepared : "` – Victor Apr 19 '20 at 15:35
  • @GrandPhuba in that String i want to remove "Date Prepared : 14-Apr-2020 12:06", not other dates. for example if String contains other like item added date etc.. i want to keep.. i want to remove only Date Prepared : 14-Apr-2020 12:06 from String, other dates should not be removed Thanks – Vinod Kumar Apr 19 '20 at 15:46
  • Use `re.sub('Date Prepared : \d\d-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d\d\d\d \d\d:\d\d', '', "Date Prepared : 14-Apr-2020 12:06 20-Jan-2021 23:52")` and this will return `'20-Jan-2021 23:52'` – Victor Apr 19 '20 at 15:48
  • @GrandPhuba Thanks, above solution worked fine for me can you please help combining both in one new_string = re.sub('\d\d/\d\d/\d\d \d\d:\d\d', '', string) re.sub('Date Prepared : \d\d-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d\d\d\d \d\d:\d\d', '', "Date Prepared : 14-Apr-2020 12:06 20-Jan-2021 23:52") how to combine both regex in one for all types of strings? Thanks – Vinod Kumar Apr 19 '20 at 17:22
  • You can wrap each expression in parentheses and use the pipe `|` to combine expressions `(A)|(B)` – Victor Apr 19 '20 at 17:25
  • @GrandPhuba re1 = 'Date Prepared : \d\d-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d\d\d\d \d\d:\d\d' re2 = '\d\d/\d\d/\d\d \d\d:\d\d' result = re.sub((re1)|(re2), '', String) getting error - TypeError: unsupported operand type(s) for |: 'str' and 'str' can you please help Thanks – Vinod Kumar Apr 19 '20 at 17:43
  • @VinodKumar The expressions need to be inside the regex string. Try `result = re.sub('({})|({})'.format(re1, re2), '', String)` – Victor Apr 19 '20 at 17:47
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'
ScoJo
  • 139
  • 5