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