Yesideez Posted July 8, 2007 Share Posted July 8, 2007 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 https://forums.phpfreaks.com/topic/58941-solved-matching-a-minimum-of-one-word-in-a-list/ Share on other sites More sharing options...
Yesideez Posted July 8, 2007 Author Share Posted July 8, 2007 I think I've done it .(gif|jp(e)?g|png) Seems to work OK or is this going to let anything else thorugh that I don't want? Link to comment https://forums.phpfreaks.com/topic/58941-solved-matching-a-minimum-of-one-word-in-a-list/#findComment-292468 Share on other sites More sharing options...
Yesideez Posted July 8, 2007 Author Share Posted July 8, 2007 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 https://forums.phpfreaks.com/topic/58941-solved-matching-a-minimum-of-one-word-in-a-list/#findComment-292491 Share on other sites More sharing options...
Wildbug Posted July 8, 2007 Share Posted July 8, 2007 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://. Link to comment https://forums.phpfreaks.com/topic/58941-solved-matching-a-minimum-of-one-word-in-a-list/#findComment-292507 Share on other sites More sharing options...
Yesideez Posted July 8, 2007 Author Share Posted July 8, 2007 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 https://forums.phpfreaks.com/topic/58941-solved-matching-a-minimum-of-one-word-in-a-list/#findComment-292523 Share on other sites More sharing options...
Wildbug Posted July 8, 2007 Share Posted July 8, 2007 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 https://forums.phpfreaks.com/topic/58941-solved-matching-a-minimum-of-one-word-in-a-list/#findComment-292550 Share on other sites More sharing options...
Yesideez Posted July 8, 2007 Author Share Posted July 8, 2007 Many thanks for clearing that up - really appreciated! Link to comment https://forums.phpfreaks.com/topic/58941-solved-matching-a-minimum-of-one-word-in-a-list/#findComment-292658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.