1

I have created a list which has day no. and the respective timing an employee has logged in. I want to find out on which days the employee has not logged any timings denoting he/she has taken a holiday.

pattern = ["Timings: "]
timingData = ['Day: 1.0 Timings: 09:52 17:46 ', 'Day: 2.0 Timings: 09:29 09:29 17:54 ', 'Day: 3.0 Timings: 09:28 09:28 17:42 ', 'Day: 4.0 Timings: 11:18 17:47 ', 'Day: 5.0 Timings: ', 'Day 6.0 Timings: ']

Using regex I want to find which Timings do not have digits after them and update the counter or log the position accordingly to count the number of holidays.

So Day: 5.0 and Day: 6.0 should be logged as holidays.

I tried online regex which worked. But I just can't figure out how do I implement it on my local editor. Link

3
  • 2
    Would love to see what you've tried first ;-) Commented Dec 21, 2018 at 5:32
  • The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a minimal reproducible example. For more information, please see How to Ask and take the tour. Commented Dec 21, 2018 at 5:34
  • I tried this but I don't know how do I implement it in editor. Commented Dec 21, 2018 at 5:37

4 Answers 4

2

You don't necessarily need regular expressions here. What if you would just split on "Timings:" and check what you've got in the result of a split. Looks clean to me:

In [1]: timingData = ['Day: 1.0 Timings: 09:52 17:46 ', 'Day: 2.0 Timings: 09:29 09:29 17:54 ', 'Day: 3.0 Timings: 09:28 09:28 17:42 ', 'Day: 4.0 Timings: 11:18 17:47 ', 'Day: 5.0 Timings: ', 'Day 6.0 Timings: ']

In [2]: for item in timingData:
            day, timing = item.split("Timings: ")
            if not timing:
                print(day)        
Day: 5.0 
Day 6.0 

There is, of course, this assumption here that the items in the list follow this specific pattern.

Sign up to request clarification or add additional context in comments.

5 Comments

So it splits the elements if there is a space after timings right? And if it cannot split then the respected day is printed right?
@NeerajLagwankar yeah, basically, if there is nothing after Timings: , we print out the day.
This is a pretty neat solution
@silverhash yeah, I've got my regex gold badge providing non-regex answers to regex questions :D
I really wanted to accept this as an answer but I couldn't since the ques was for regex. Thanks for this neat solution though. @alecxe
1

In your posted string, you have word Timings: but your regex only contains Timing: which obviously won't match and looks like its a typo from your end.

You can use a simple regex, where if Timings: is followed by an optional space and at least two digits, then you can consider that timing data is present else not.

Here is a python code for same,

import re

timingData = ['Day: 1.0 Timings: 09:52 17:46 ', 'Day: 2.0 Timings: 09:29 09:29 17:54 ', 'Day: 3.0 Timings: 09:28 09:28 17:42 ', 'Day: 4.0 Timings: 11:18 17:47 ', 'Day: 5.0 Timings: ', 'Day 6.0 Timings: ']

for s in timingData:
 if (re.match(r'.*Timings:\s*\d{2}:.*', s)):
  print (s + ' --> ' + "Matched")
 else:
  print (s + ' --> ' + "Didn't match")

Which gives following output like you must expect,

Day: 1.0 Timings: 09:52 17:46  --> Matched
Day: 2.0 Timings: 09:29 09:29 17:54  --> Matched
Day: 3.0 Timings: 09:28 09:28 17:42  --> Matched
Day: 4.0 Timings: 11:18 17:47  --> Matched
Day: 5.0 Timings:  --> Didn't match
Day 6.0 Timings:  --> Didn't match

1 Comment

yeah it is typing error from my end. I'll update it.
1

As already pointed out by @alecxe, regex in this problem is overkill but If you really need to, I guess you could simply do something like:

import re
holidays = []

timingData = ['Day: 1.0 Timings: 09:52 17:46 ', 'Day: 2.0 Timings: 09:29 09:29 17:54 ', 'Day: 3.0 Timings: 09:28 09:28 17:42 ', 'Day: 4.0 Timings: 11:18 17:47 ', 'Day: 5.0 Timings: ', 'Day 6.0 Timings: ']

for t in timingData:
    a = re.search('(Timings:\s)[\w:\s]+',t)
    if a == None: #No matches found
            holidays.append(t[:8])
print(holidays)

Comments

0
import re
timingData = ['Day: 1.0 Timings: 09:52 17:46 ', 'Day: 2.0 Timings: 09:29 09:29 17:54 ', 'Day: 3.0 Timings: 09:28 09:28 17:42 ', 'Day: 4.0 Timings: 11:18 17:47 ', 'Day: 5.0 Timings: ', 'Day 6.0 Timings: ']

regexedData = []
for i in timingData:
    regexedData.append(re.findall(r'(.+?)(Timings:\s)$',i))
for i in regexedData:
    if i:
        print(i[0][0])

Output:

C:\Users\Desktop>py x.py
Day: 5.0
Day 6.0

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.