Jump to content

Search by string length


perky416

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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}\\\.'");

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.