Skip to content

Instantly share code, notes, and snippets.

@Nyahua
Created March 27, 2017 23:22
Show Gist options
  • Save Nyahua/9521866304ea99aeb52f12dd4f710bf6 to your computer and use it in GitHub Desktop.
Save Nyahua/9521866304ea99aeb52f12dd4f710bf6 to your computer and use it in GitHub Desktop.
regular expression for 7 digits between special words

I got a wrong log as:

test_string = """
03/27/17 12:55.23something wrong with 20405 post id 450469503/27/17 12:50.55something
wrong with 20302 post id 450180903/27/17 11:41.18something wrong with 18314 
post id 443624203/27/17 11:26.56something wrong with 17922 post id 442210904/27/17 10:48.33
something wrong with 16775 post id 438259104/27/17 10:30.59something 
wrong with 16275 post id 436333705/27/17 
"""

I want to get the exact post id after "post id ", but not the datatime information. soltion:

import re
test_string = """
03/27/17 12:55.23something wrong with 20405 post id 450469503/27/17 12:50.55something
wrong with 20302 post id 450180903/27/17 11:41.18something wrong with 18314 
post id 443624203/27/17 11:26.56something wrong with 17922 post id 442210904/27/17 10:48.33
something wrong with 16775 post id 438259104/27/17 10:30.59something 
wrong with 16275 post id 436333705/27/17 
"""

regex = r'(?<=post id\s)(\d*)(?=\d\d/\d\d/\d\d)'
re.compile(regex).findall(test_string)

http://www.pyregex.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment