Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In my date time value I want to use regex to strip out the slash and colon from time and replace it with underscore

I am using Python, Webdriver for my automated test. My scenario is on the Admin page of our website I click Add project button and i enter a project name.

Project Name I enter is in the format of LADEMO_IE_05/20/1515:11:38 It is a date and time at the end.

What I would like to do is using a regex I would like to find the / and : and replace them with an underscore _

I have worked out the regex expression:

[0-9]{2}[/][0-9]{2}[/][0-9]{4}:[0-9]{2}[:][0-9]{2}

This finds 2 digits then / followed by 2 digits then / and so on.

I would like to replace / and : with _.

Can I do this in Python using import re? I need some help with the syntax please.

My method which returns the date is:

def get_datetime_now(self):
            dateTime_now = datetime.datetime.now().strftime("%x%X")
            print dateTime_now #prints e.g. 05/20/1515:11:38
            return dateTime_now

My code snippet for entering the project name into the text field is:

project_name_textfield.send_keys('LADEMO_IE_' + self.get_datetime_now())

The Output is e.g.

LADEMO_IE_05/20/1515:11:38

I would like the Output to be:

LADEMO_IE_05_20_1515_11_38 
like image 978
Riaz Ladhani Avatar asked Nov 30 '22 17:11

Riaz Ladhani


2 Answers

Just format the datetime using strftime() into the desired format:

>>> datetime.datetime.now().strftime("%m_%d_%y%H_%M_%S")
'05_20_1517_20_16'
like image 195
alecxe Avatar answered Dec 03 '22 05:12

alecxe


Another simple option is just using string replace :

s = "your time string"
s = s.replace("/", "_").replace(":", "_")
like image 34
Guy Levin Avatar answered Dec 03 '22 06:12

Guy Levin