Jump to content

Filename search


jaikob

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/213763-filename-search/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/213763-filename-search/#findComment-1112615
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/213763-filename-search/#findComment-1112617
Share on other sites

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 : )

Link to comment
https://forums.phpfreaks.com/topic/213763-filename-search/#findComment-1112621
Share on other sites

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\_\-]

Link to comment
https://forums.phpfreaks.com/topic/213763-filename-search/#findComment-1112631
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/213763-filename-search/#findComment-1112634
Share on other sites

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\_\-\.\,]

Link to comment
https://forums.phpfreaks.com/topic/213763-filename-search/#findComment-1112645
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.