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"?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.