Regular expression for file extension

I am wondering if there is a regular expression that I can use to search for file extensions using basic html.

I have now

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

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

So, I would like the regex to be able to find all images starting with small and ending with .jpg , .jpeg , .jpg , .png , .gif etc ... both lowercase and uppercase.

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

I'm not sure what I'm doing wrong, but when I try to make any suggestions, the output shows only the regular expression, not the intended results. here is 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('Ymd H:i',$item['cdate']); $tmpl_added_by=$item['added_by']; $tmpl_modified_by=$item['modified_by']; // $tmpl_date_mod=date('Ymd 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 am i doing wrong?

+5
source share
3 answers

A regular expression to capture all images starting with "small" and ending with any extension that you specify:

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

Explanations:

^ - starts with

.* - matches 0 or more characters

\. - corresponds to the period; \ is an escape character, which means that the next character should be interpreted literally, which is necessary because the characters . , ? , + etc. make sense in regex otherwise.

(a|b|c) - matches everything contained in parentheses (for example, matches if the next character is a or b or c )

? - means that the previous character can be displayed as zero or once (for example, jpe?g matches "jpeg" because it contains one "e", and also matches "jpg" because "e" appears zero time in this position)

$ - indicates the end of the pattern to be matched

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

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

+2
source

Perhaps this will help you?

 /\.\w+$/ 

(dot character and 1 (or more) characters of the word class at the end of the input line)

or

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

(dot character and 1 (or more) letters at the end of the input line)

or

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

(dot character and 1 to 4 letters at the end of the input line)

Note that all parameters have a dotted sign output.
All regular expressions are case insensitive since i -flag exists.


Also, unfortunately, JavaScript does not support lookbehind, so regexp

 /(?<=small\.)[az]+/i 

(1 or more letters after the substring small. )

will not work; but I think that would be the best regexp in this case.

+1
source

According to this page from www.Regular-Expressions.info , case sensitivity and other similar flags / options are usually implemented in the programming language, and not directly in the template itself. However, if the language does not provide the ability to pass flags / options / flags, the RegEx page linked above indicates that parameters such as case insensitivity can be specified in the template.

To specify a pattern as case-insensitive, you can specify (?i) as the beginning of the case-insensitive part, and (?-i) as the end of the case-insensitive part.

In your specific situation, this means that the following RegEx pattern may be effective: \.(?i)(png|gif|jpg|jpeg|ico|bmp|svg|tiff)(?-i)$ Obviously, you will want to change this template to your needs to add or remove image file types.

In this template: \. indicates a literal period symbol ( . ); (?i) disables case sensitivity for all the following parts; (png|gif|jpg|jpeg|ico|bmp|svg|tiff) denotes a list of options, that is, the file extension must be one (and only one) of the options in the list; (?-i) includes case sensitivity; and $ denotes the end of the line, that is, there cannot be any characters following the file extension.

If the language you use supports specifying matching flags in another way, I would advise you to specify them in any way that the language recommends; however, according to the RegEx info page above, this is the right way to specify them in the template (which does not allow the case to be case insensitive if you decide to add more to the template).

EDIT : As a warning, it seems that Javascript does not support turning on or off case sensitivity inside the template itself (using (?i) and (?-i) ). So, if you are working with Javascript, you will have to get around this by setting /i as the flag at the end of your expression or using a template such as: \.([pP][nN][gG]|[jJ][pP][eE]?[gG]|[gG][iI][fF]|[iI][cC][oO]|[tT][iI][fF]{1,2})$ . In this template, each letter in the extension is indicated in both upper and lower case. This allows the template to match the file extension regardless of the case in which it is written, but the rest of the expression is case sensitive.

0
source

Source: https://habr.com/ru/post/1238034/


All Articles