Jump to content

[SOLVED] Matching a minimum of one word in a list


Yesideez

Recommended Posts

I've tried learning regex before and never been able to. With the help of www.regular-expressions.info reading slowly through it I've started to pick it up and seem to be able to read quite a few.

 

Just been thinking, if I wanted to check a link was a valid picture (only GIF, JPEG, JPG, PNG) what expression would I use?

 

The interesting part I see is with JPEG and JPG as one is only minus an "E".

 

What I don't want to do is match BMP, TIF or anything else like TXT or even random characters.

 

Ideally case insensitive.

 

My initial thought is

(gif|jpeg|jpg|png)

 

Is there a way of simplyflying that, like specifying an option "e" instead of specifying both "jpg" and "jpeg"?

OK my final regex string I've made is this:

^w{3}\..+/gfx/.+\.(gif|jpe??g|png)$

 

What I'm hoping this does is check the file is from "www." and nothing else (like "ftp."), is in a folder called "gfx" and is any of the valid files listed above. Is this right or will it let anything else through?

To match the extention:

 

/\.(?:gif|png|jpe?g)$/i

 

To answer your third regex:

 

|^http://.+?/gfx/.+?\.(?:gif|png|jpe?g)?|i

 

HTTP servers can have names different than www.  It could be images.server.com or just http://server.com.  The better way to check is to check the http:// protocol.  If it's ftp, then it's ftp://.

Thanks for the reply - just a little confused for the following:

 

1. First "/" and ending "/i"

2. Use of "?:"

3. Second  "|" and ending "|i"

 

In RegexBuddy it shows the single "." not being escaped means any character ???

1. First "/" and ending "/i"

2. Use of "?:"

3. Second  "|" and ending "|i"

 

1. & 3.  Regular expressions are enclosed within delimiters.  "//" are the default/most common delimiters, but really you can use almost anything.  Since a delimiter is a special character, if it is used within a regular expression it must be escaped.  When using "//" with HTML-heavy expressions, the escaping can be confusing and make the result hard to read, e.g. /http:\/\/www.stuff.com\/a\/\.html/.  I like to use the pipe character ("|") when I'll be using the slash ("/") in the pattern alot.

 

The "i" is a modifier which means case-INsensitive, so "HTML" and "html" both match "/html/i"

 

2.  Normally, parentheses are used both to capture and to group.  If you want to group, but do NOT want to capture,  you can use "(?:".  For instance, /(\d{4})/ captures four digits and stores them in $1 (or in a variable like $match if you're using preg_match(..., $match)).  /abc(?:123|xyz)/ matches either "abc123" or "abcxyz", but does not store the result.

 

In RegexBuddy it shows the single "." not being escaped means any character ???

 

Correct, with one exception:  "." won't match a newline (\n) unless you use the /s modifier.

 

 

See Pattern Modifiers for more information about /i and /s and all their friends.

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.