-4

I am working with the regex library in Python. I'm currently creating a condition which I want to bypass below sample string because I read a file line by line.

I would like to bypass the string if I found a format like:

03/27/2016 07:58:17.442

Sample string to bypass:

03/27/2016 07:58:17.442   U:Event:   Current Process Recipe Name = PFCA-800Pulse.prc [LLA_01] [2016-03-27_003_A1B]

My current code:

    self.matchObj = re.match( r'([0-9])\w+', l)
    if not self.matchObj:
       #do the following code here
halfer
  • 19,824
  • 17
  • 99
  • 186
iamcoder
  • 529
  • 2
  • 4
  • 23
  • 1
    It seems you forgot to include a question in your question. – Biffen Feb 15 '17 at 05:45
  • Can you give a better idea of the strings that you're trying to skip. Which parts of that string are common? Which parts are unique? Are there any other strings that look similar to that one, but that you don't want to bypass. – Batman Feb 15 '17 at 05:47
  • 1
    What part of that string is supposed to be matching the regex? Because as written, it will match (`03` is a digit followed by a word-like character, since word-like characters include letters and digits), but if all your lines are date stamped like that, it would skip all of them. You need to specify what you want to skip, and what you want to keep. Without a set of things to keep, you could just use the the regex `r''` and you'd skip everything, which meets your specs as given. – ShadowRanger Feb 15 '17 at 05:48
  • You may try building the regex using https://regex101.com/ It has all the help that you may need as a begineer – ZdaR Feb 15 '17 at 05:48
  • Possible duplicate of [Learning Regular Expressions](http://stackoverflow.com/questions/4736/learning-regular-expressions) – Biffen Feb 15 '17 at 05:50
  • How did you get on with the below answer, iamcoder? – halfer Aug 27 '17 at 19:31

1 Answers1

0

Try this:

    self.matchObj = re.match( r'^[0-9/]{10}.+$', l)

https://regex101.com/r/kAsQUn/1

EDIT Based on comments:

    self.matchObj = re.match( r'\d\d/\d\d/\d\d\d\d \d\d:\d\d:\d\d\.\d\d\d', l)

https://regex101.com/r/kAsQUn/2

MotKohn
  • 3,485
  • 1
  • 24
  • 41
  • 2
    That doesn't check for the timestamp, just the date, and it doesn't actually check for a date, just a run of digits and slashes (`1234567890abc` would match, as would `///////////`). Side-note: You don't need `^` when using `re.match` (`^` is implicit for `match`, it's the only real difference between `match` and `re.search`, [with some special behaviors in multiline mode](https://docs.python.org/3/library/re.html#search-vs-match)) and `.*$` can be omitted if you're just testing, not capturing (`.+$` is different, but I don't see why it's needed). – ShadowRanger Feb 15 '17 at 05:54
  • @ShadowRanger You are right just following OP's lead. It seems to me that that's what s/he was trying to do. But good comment. – MotKohn Feb 15 '17 at 05:57
  • @MotKohn Hi Sir yes i just want only to check the string if has a format similar to this 03/27/2016 07:58:17.442 – iamcoder Feb 15 '17 at 06:00