Search code examples
pythonpython-re

How to get all the text before and after some characters using regular expressions


How can I get some characters from an url, using regular expressions for example:

https://www.facebook.com/aaaaaa/posts/123456789/sdd

How can I get the characters aaaa and 123456789 that are before and after /posts/ ?


Solution

  • import re
    
    url = "https://www.facebook.com/aaaaaa/posts/123456789/sdd"
    
    out = re.match('.+/(.+)/posts/(.+)/', url).groups()
    
    print(out)
    
    # Output:
    
    ('aaaaaa', '123456789')