Split string with regex not working
Question: I'm trying to split big file with some regex. Problem is that I want to keep
delimiter in text after split, and I tried to add ?= on the beggining of
regex, but then it doesn't split. I tried modified regex in Sublime, and it's
working there.

Text is like this:  
  
Aug 07, 2014 01:01:01 PM  
some text  
Aug 07, 2014 02:02:02 PM  
  
  
So, date, then some text and date. I want to get split text with regex which
recognize that date.

First version of regex, which works perfectlly for my purpose:

    
    
    \w{3}\s\d{2}\,\s\d{4}\s\d{1,2}\:\d{2}\:\d{2}\s[AM|PM].)
    

Code in Python is this:

    
    
    allparts = re.compile(r'\w{3}\s\d{2}\,\s\d{4}\s\d{1,2}\:\d{2}\:\d{2}\s[AM|PM].').split(alltext)
    

After adding ?=, it looks like this:

    
    
    allparts2 =re.compile(r'(?=\w{3}\s\d{2}\,\s\d{4}\s\d{1,2}\:\d{2}\:\d{2}\s[AM|PM].)').split(alltext)
    

What I'm doing wrong in second code?


Answer: Sorry, my first answer was wrong:) Try not adding ?=, only put it in
parentheses like this:

    
    
    allparts2 =re.compile(r'(\w{3}\s\d{2},\s\d{4}\s\d{1,2}\:\d{2}\:\d{2}\s[AM|PM].)').split(alltext)
    

Then try it without compile...

    
    
    allparts2 = re.split('(\w{3}\s\d{2},\s\d{4}\s\d{1,2}\:\d{2}\:\d{2}\s[AM|PM].)', alltext)
    

When using:

    
    
    #!/usr/local/bin/python2.7
    import re
    
    alltext = "Aug 07, 2014 01:01:01 PM some text Aug 07, 2014 02:02:02 PM another text Aug 07, 2014 03:03:03 AM " 
    
    allparts2 = re.split('(?=\w{3}\s\d{2},\s\d{4}\s\d{1,2}\:\d{2}\:\d{2}\s[AM|PM].)', alltext)
    print(allparts2)
    

Result was:

    
    
    Executing the program....
    $python2.7 main.py
    ['Aug 07, 2014 01:01:01 PM some text Aug 07, 2014 02:02:02 PM another text Aug 07, 2014 03:03:03 AM ']
    

When using:

    
    
    #!/usr/local/bin/python2.7
    import re
    
    alltext = "Aug 07, 2014 01:01:01 PM some text Aug 07, 2014 02:02:02 PM another text Aug 07, 2014 03:03:03 AM "
    
    
    allparts2 = re.split('(?:\w{3}\s\d{2},\s\d{4}\s\d{1,2}\:\d{2}\:\d{2}\s[AM|PM].)', alltext)
    
    print(allparts2)
    

Result was:

    
    
    Executing the program....
    $python2.7 main.py
    ['', ' some text ', ' another text ', ' ']
    

When using:

    
    
    #!/usr/local/bin/python2.7
    import re
    
    alltext = "Aug 07, 2014 01:01:01 PM some text Aug 07, 2014 02:02:02 PM another text Aug 07, 2014 03:03:03 AM "
    
    
    allparts2 = re.split('(\w{3}\s\d{2},\s\d{4}\s\d{1,2}\:\d{2}\:\d{2}\s[AM|PM].)', alltext)
    
    print(allparts2)
    

Result was:

    
    
    Executing the program....
    $python2.7 main.py
    ['', 'Aug 07, 2014 01:01:01 PM', ' some text ', 'Aug 07, 2014 02:02:02 PM', ' another text ', 'Aug 07, 2014 03:03:03 AM', ' ']
    

Just to compare different forms.

