perky416 Posted May 8, 2011 Share Posted May 8, 2011 Hi everyone, Me again with yet another regex problem lol. Ok iv been trying to put regex together by myself as im trying to learn. I am trying to use regex to filter search results by the length of the string. So if im trying to search for "test.com" using "^.{0,8}$" returns it correctly. However i cant figure out how to make it search for the string length before the 1st decimal point of the domain. Im trying to get it so that using "^.{0,4}$" should return "test.com" Any ideas? Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235843-search-by-string-length/ Share on other sites More sharing options...
.josh Posted May 8, 2011 Share Posted May 8, 2011 "^.{0,4}\." Quote Link to comment https://forums.phpfreaks.com/topic/235843-search-by-string-length/#findComment-1212398 Share on other sites More sharing options...
perky416 Posted May 8, 2011 Author Share Posted May 8, 2011 Hi mate, Thanks a lot for the code however it doesnt work. The search is pulling up results longer than 4 characters. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235843-search-by-string-length/#findComment-1212482 Share on other sites More sharing options...
.josh Posted May 8, 2011 Share Posted May 8, 2011 You said you wanted to test the string length before the first decimal. You then provided an example regex that showed 0-4 chars, and showed that even though "test.com" should match, presumably because even though " test.com" is 8 characters long, "test" is 4 characters. The pattern starts at the beginning of the string and matches 0-4 characters and then a literal dot. Yes, it will match for instance "test.com" or "test.someotherrandomstuff", because that is what you asked for. If this is not doing what you expect, then be more specific about what it is you are wanting to do and provide several examples. Quote Link to comment https://forums.phpfreaks.com/topic/235843-search-by-string-length/#findComment-1212524 Share on other sites More sharing options...
perky416 Posted May 9, 2011 Author Share Posted May 9, 2011 What you describe is what im trying to achieve, however when i put what you gave me into a mysql query it is pulling up more than 4 characters before the decimal. It matches test.com, test1.com, test10.com, testing.com. All of these have more than 4 characters before the decimal. Quote Link to comment https://forums.phpfreaks.com/topic/235843-search-by-string-length/#findComment-1212846 Share on other sites More sharing options...
.josh Posted May 9, 2011 Share Posted May 9, 2011 post your query string Quote Link to comment https://forums.phpfreaks.com/topic/235843-search-by-string-length/#findComment-1212858 Share on other sites More sharing options...
perky416 Posted May 9, 2011 Author Share Posted May 9, 2011 mysql_query("SELECT domain FROM domains WHERE domain REGEXP '^.{0,4}\.'"); Thanks mate. Quote Link to comment https://forums.phpfreaks.com/topic/235843-search-by-string-length/#findComment-1212866 Share on other sites More sharing options...
.josh Posted May 9, 2011 Share Posted May 9, 2011 oh okay, you need to double escape: '^.{0,4}\\.' Quote Link to comment https://forums.phpfreaks.com/topic/235843-search-by-string-length/#findComment-1212886 Share on other sites More sharing options...
perky416 Posted May 9, 2011 Author Share Posted May 9, 2011 Thanks i appreciate your help, however it still doesn't seem to be working. Im not sure if this has anything to do with it but i have been echo'ing out the query for development purposes, and although i have added the extra \ only 1 is being displayed in the echo. Any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235843-search-by-string-length/#findComment-1212897 Share on other sites More sharing options...
.josh Posted May 9, 2011 Share Posted May 9, 2011 gah, okay, I just ran it on a test table in my own phpmyadmin. Upon further reflection, since you are running from php and inside double quotes, you actually have to escape the escape too mysql_query("SELECT domain FROM domains WHERE domain REGEXP '^.{0,4}\\\.'"); Quote Link to comment https://forums.phpfreaks.com/topic/235843-search-by-string-length/#findComment-1212900 Share on other sites More sharing options...
perky416 Posted May 9, 2011 Author Share Posted May 9, 2011 PERFECT!!! Thank you as I said I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/235843-search-by-string-length/#findComment-1212906 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.