Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression for File Extension [closed]

Tags:

html

regex

I'm wondering if there is a regular expression I can use to find file extensions using basic html.

Right now i have

<img src="images/pimage/{$item['item_id']}/small.jpg">

The problem is not all my images end in .jpg.

So I would like a regular expresion that can find all images that start with small and end in .jpg, .jpeg, .JPG, .png, .gif, etc... in both lower case and upper case.

Is there a simple regex I can put after "small" to find all matching files?

I'm not sure what I'm doing wrong, but when I try any of the suggestions, the output show only the regex, and not the intended results. here's my current code

        function print_item($item,$link_change){
        $acticon= ($item['active']==TREEMAN_ITEM_ACTIVE)? 'active.gif' : 'disabled.gif';
        $tmpl_act=($this->section->webmode)? "<td><img src=\"".ADMIN_IMG_PATH."icons/{$acticon}\"></td>" : "";
        $tmpl_date=date('Y-m-d H:i',$item['cdate']);
        $tmpl_added_by=$item['added_by'];
        $tmpl_modified_by=$item['modified_by'];
//      $tmpl_date_mod=date('Y-m-d H:i',$item['timestamp']);
        $tmpl_date_mod=$item['timestamp'];
        $res=<<<EOT
<td >{$item['item_id']}</td>
<td><a href="{$link_change}"</a><img src="/img/ucart/images/pimage/{$item['item_id']}/small.jpg" height="75" width="75"</td>
<td class="tb" width="50%"><a href="{$link_change}" title="Edit item">{$item['item_name']}</a></td>
<td>$tmpl_date</td>
<td>$tmpl_date_mod</td>
<td>$tmpl_added_by</td>
<td>$tmpl_modified_by</td>
$tmpl_act
EOT;

any idea what i'm doing wrong?

like image 207
Robert Avatar asked Dec 13 '15 20:12

Robert


People also ask

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .

What is the use of \\ in regex?

You also need to use regex \\ to match "\" (back-slash). Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.

What is a regex extension?

The regular expression extensions are a way to significantly add to the power of patterns without adding a lot of meta-characters to the proliferation that already exists. By using the basic (?...) notation, the regular expression capabilities can be greatly extended.

What is whitespace regular expression?

The most common forms of whitespace you will use with regular expressions are the space (␣), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces.


2 Answers

The regex to capture all images that begin with "small" and end in any extension that you've listed:

/^small.*\.(jpe?g|gif|png|tiff)$/i

Terms Explained:

^ - starts with

.* - matches 0 or more of any character

\. - matches a period; \ is an escape character that means the following character should be interpreted literally, which is necessary because the characters ., ?, +, etc. have a meaning in regex otherwise.

(a|b|c) - matches anything contained inside the parentheses (example, matches if the next character is a or b or c)

? - signifies that the preceding character may appear zero or one time (example, jpe?g matches "jpeg" because it contains one 'e', and also matches "jpg" because the 'e' appears zero times in that position)

$ - denotes end of pattern to be matched

/your-regex-pattern/i - case insensitive matches (upper and lowercase)

If you want to learn more about pattern matching in regex, check out this handy cheatsheet: http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

like image 134
femmestem Avatar answered Oct 19 '22 22:10

femmestem


Maybe, this will help you?

/\.\w+$/

(the dot sign and 1 (or more) word class characters at the end of input string)

or

/\.[a-z]+$/i 
/\.[a-zA-Z]+$/

(the dot sign and 1 (or more) letters at the end of input string)

or

/\.[a-z]{1,4}$/i
/\.[a-zA-Z]{1,4}$/

(the dot sign and 1 to 4 letters at the end of input string)

Notice, that all options has a dot sign in output.
All regexps are case-insensitive since there is an i-flag.


Also, unfortunately, JavaScript doesn't support lookbehind, therefore regexp

/(?<=small\.)[a-z]+/i

(1 or more letters after small. substring)

won't work; but, I think, it would be the best regexp in this case.

like image 41
Dima Parzhitsky Avatar answered Oct 19 '22 22:10

Dima Parzhitsky