Regex: (?:(?:threads/[^.]*\.|showthread\.php\?t=)(?[0-9]*)(?:[^\n-]*-(?[0-9]*))?|posts/|showthread\.php\?p=)(?:(?:#post-)?(?[0-9]*))? Test text: [OTHER TEXT DON'T TOUCH]http://forums.spacebattles.com/threads/blahblah.1234/page-56#post-789[OTHER TEXT DON'T TOUCH] FILLER TEXT [OTHER TEXT DON'T TOUCH]http://forums.spacebattles.com/threads/blahblah.1234/page-56[OTHER TEXT DON'T TOUCH] FILLER TEXT [OTHER TEXT DON'T TOUCH]http://forums.spacebattles.com/threads/blahblah.1234[OTHER TEXT DON'T TOUCH] FILLER TEXT [OTHER TEXT DON'T TOUCH]http://forums.spacebattles.com/threads/.1234/page-56#post-789[OTHER TEXT DON'T TOUCH] FILLER TEXT [OTHER TEXT DON'T TOUCH]http://forums.spacebattles.com/threads/.1234/page-56[OTHER TEXT DON'T TOUCH] FILLER TEXT [OTHER TEXT DON'T TOUCH]http://forums.spacebattles.com/threads/.1234[OTHER TEXT DON'T TOUCH] FILLER TEXT [OTHER TEXT DON'T TOUCH]http://forums.spacebattles.com/posts/789[OTHER TEXT DON'T TOUCH] FILLER TEXT [OTHER TEXT DON'T TOUCH]http://forums.spacebattles.com/showthread.php?t=1234[OTHER TEXT DON'T TOUCH] FILLER TEXT [OTHER TEXT DON'T TOUCH]http://forums.spacebattles.com/showthread.php?p=789[OTHER TEXT DON'T TOUCH] FILLER TEXT Regex breakdown: (?: # Noncapturing group containing the thread and post. Nonoptional (?: # Optional group containing the thread identifiers threads/[^.]*\. # threads/name-of-thread. | # or showthread\.php\?t= # showthread.php?t= ) # (?[0-9]*) # Named group containing the thread ID (?: # Optional group containing the page number [^\n-]*- # /page- (?[0-9]*) # Named group containing the page number )? # | # or posts/ # posts/ | # or showthread\.php\?p= # showthread.php.php?p= ) # (?: # Optional group containing the post ID (?:#post-)? # #post- (?[0-9]*) # Named group containing the post ID )? #