dontpanic Posted November 19, 2010 Share Posted November 19, 2010 I'm trying to match something along the lines of: "name-username:password@server," and I can do that with this regex: $pattern = '/(?P<name>\w+)-(?P<username>\w+)?P<password>\w+)@(?P<server>\w+)/' But things start going wrong with a password that contains one of the delimiter characters. I could change the password \w+ to something more specific ([a-zA-Z0-9!@#$^&*()_-=+]) but something in my gut tells me thats not right. I don't have enough experience with regular expressions to tackle this and was wondering if anyone here has any ideas. This format (other than the first '-') is commonly used when logging into websites, so I imagine its been parsed before. I think I need a sort of greedy negative look-behind, but I just can't seem to grasp how to go about it. Thanks in advance for your help! Update: here is a list of non-alphanumeric characters that passwords may contain : ~!@#$%^&*()_+{}|:"<>?/.,';\][=-`) Quote Link to comment https://forums.phpfreaks.com/topic/219220-find-the-last-occurrence-of-a-character/ Share on other sites More sharing options...
sasa Posted November 19, 2010 Share Posted November 19, 2010 try $pattern = '/(?P<name>^[^-]+)-(?P<username>[^:]+)?P<password>[^@]+)@(?P<server>.*$)/'; Quote Link to comment https://forums.phpfreaks.com/topic/219220-find-the-last-occurrence-of-a-character/#findComment-1136842 Share on other sites More sharing options...
dontpanic Posted November 19, 2010 Author Share Posted November 19, 2010 That got me closer, but not quite there... $pattern = '/(?P<name>^[^-]+)-(?P<username>[^:]+)?P<password>[^@]+)@(?P<server>.*$)/'; $subject = 'name-username:p@ssword@server'; preg_match($pattern, $subject, $matches); print_r($matches); yields Array ( [0] => name-username:p@ssword@server [name] => name [1] => name [username] => username [2] => username [password] => p [3] => p [server] => ssword@server [4] => ssword@server ) I do feel like we're really close though... Quote Link to comment https://forums.phpfreaks.com/topic/219220-find-the-last-occurrence-of-a-character/#findComment-1136870 Share on other sites More sharing options...
sasa Posted November 19, 2010 Share Posted November 19, 2010 ups try $pattern = '/(?P<name>^[^-]+)-(?P<username>[^:]+)?P<password>.+)@(?P<server>[^@]+$)/'; $subject = 'name-username:p@ssword@server'; preg_match($pattern, $subject, $matches); print_r($matches); ?> Quote Link to comment https://forums.phpfreaks.com/topic/219220-find-the-last-occurrence-of-a-character/#findComment-1136890 Share on other sites More sharing options...
dontpanic Posted November 19, 2010 Author Share Posted November 19, 2010 ups try $pattern = '/(?P<name>^[^-]+)-(?P<username>[^:]+)?P<password>.+)@(?P<server>[^@]+$)/'; $subject = 'name-username:p@ssword@server'; preg_match($pattern, $subject, $matches); print_r($matches); ?> That did it! I can't believe how much I was over thinking that ":<password>-" part. Thanks a ton, sasa! Quote Link to comment https://forums.phpfreaks.com/topic/219220-find-the-last-occurrence-of-a-character/#findComment-1136894 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.