jaikob Posted September 18, 2010 Share Posted September 18, 2010 I'm trying to create a regex to search a blob of files. I have this as regex: $param = "test"; $reg = "\b".$param.".(\w*)(\w|[-.])+$"; It currently will return the result if the filename starts with test, but it does not acknowledge the position of the keyword, and if the filename contains multiple words and spaces. So if I have test1.css test2.css test copy.css test copy with more words.css copy test.css It will only return test1.css, test2.css, and test copy.css. How can I fix this regex to work the way I want it? Thank you. Quote Link to comment Share on other sites More sharing options...
jaikob Posted September 18, 2010 Author Share Posted September 18, 2010 It also doesn't acknowledge if there is punctuation after the file name, and capitalization. Test.css Test, Test.css Will not match. Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 18, 2010 Share Posted September 18, 2010 I am not extremely good yet at this, but I want to help. In order to do so just to make things clear. Your regex should return true in case: it's a file name with spaces/underscores/dashes multiple words lower and uppercase and various extensions? all starting with your parameter. If so let me know and ill start to puzzle Quote Link to comment Share on other sites More sharing options...
jaikob Posted September 18, 2010 Author Share Posted September 18, 2010 I am not extremely good yet at this, but I want to help. In order to do so just to make things clear. Your regex should return true in case: it's a file name with spaces/underscores/dashes multiple words lower and uppercase and various extensions? all starting with your parameter. If so let me know and ill start to puzzle Yes exactly, I really appreciate your help because I suck at regexp and I have no ambition to learn how to use it correctly haha. Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 18, 2010 Share Posted September 18, 2010 hehe nice, I started yesterday and I do have that ambition lols. be back in a sec ; ) Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 18, 2010 Share Posted September 18, 2010 I haven't tested it yet but this is what i came up with: By the way notice i have just placed the word test in there but it can be a variable ofc. /^(test)([.\-\s\_]*)(\.){1}(.){3,4}$/i It should read like: start with test (or if altered your variable) followed by any amount of characters dashes/underscores always end with a dot followed by 3 or 4 extension characters all case insesitive I am gonna test it now : ) Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 18, 2010 Share Posted September 18, 2010 Oh I am testing and I was thinking do you maybe know allready what extension you wanna look for or just a general look for all extensions is good enough? Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 18, 2010 Share Posted September 18, 2010 Ok i have a new one and test looks promissing, I am not sure it's foolproof but you can test it if you like: /^(test)(.*)?(\-*)?(\.){1}(.){3,4}$/i Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 19, 2010 Share Posted September 19, 2010 Alrighty here it is it works perfectly If you need help let me know : /^(test)[\w\s\_\-]*(\.){1}(\w){3,4}$/i Just for your info to explain the regex ^(test) #start with test [\w\s\_\-]* #allow zero or more word characters, spaces, underscores, dashes (\.){1} #followed by a 1 literal dot (\w){3,4}$ #followed by 3 and max 4 word characters /i #case insensitive if you like also extra dots just put \. inside [\w\s\_\-] Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 19, 2010 Share Posted September 19, 2010 Alrighty here it is it works perfectly If you need help let me know : /^(test)[\w\s\_\-]*(\.){1}(\w){3,4}$/i Just for your info to explain the regex ^(test) #start with test [\w\s\_\-]* #allow zero or more word characters, spaces, underscores, dashes (\.){1} #followed by a 1 literal dot (\w){3,4}$ #followed by 3 and max 4 word characters /i #case insensitive if you like also extra dots just put \. inside [\w\s\_\-] I changed it a little bit into /^(test)[\w\s\_\-]*(\.){1}[a-zA-Z]{3,4}$/i otherwise an underscore in the extension would be correct. And for some reason [A-z] also excepted the underscore so that why i did this. Quote Link to comment Share on other sites More sharing options...
jaikob Posted September 19, 2010 Author Share Posted September 19, 2010 Works almosttt perfect except if I have a file name of: John, Doe., Ph.D, T, 1.4.11.pdf It does not match it :/ other than that it works perfect! Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 19, 2010 Share Posted September 19, 2010 lol wtf you have files named with comma's? You should have added that to your wish list, same is for the dots. (also look at my note i gave to add dots) anyways because i like to help here you go: /^(test)[\w\s\_\-\.\,]*(\.){1}[a-zA-Z]{3,4}$/i So this is with extra dots and comma's included if you don't want comma's get rid of \, inside [\w\s\_\-\.\,] Quote Link to comment Share on other sites More sharing options...
jaikob Posted September 19, 2010 Author Share Posted September 19, 2010 Thank you! Sorry if it was a problem. Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 19, 2010 Share Posted September 19, 2010 Thank you! Sorry if it was a problem. as you would have guessed it wasn't a problem i like to help Does it work now for you? I hope it does. By the way databases aren't really designed for blobs despite the fact they support it ; ) Quote Link to comment Share on other sites More sharing options...
jaikob Posted September 19, 2010 Author Share Posted September 19, 2010 I'm fetching files from a directory and searching those with the regex. Yes you have helped me big time and thank you! Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 19, 2010 Share Posted September 19, 2010 wicked : ) if your ever interested in learning this i saw a nice video that thought me alot www.phpvideotutorials.com/regex Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 19, 2010 Share Posted September 19, 2010 ..nvm i thought the above wasn't posted but it's on second page lols Quote Link to comment 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.