Link to home
Start Free TrialLog in
Avatar of Joe Howard
Joe HowardFlag for United States of America

asked on

regex needed

Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

What's the context? You probably don't need a regex. You can do the lot with a few replaces. A lot easier to read for those that don't do regex's. Probably not as efficient as a rgex but it works, and unless you're doing millions of calls to this in one go, the efficiency will porbably not affect you.


function fixFileName(psFileName)
{
  return psFileName.replace('https://d1yl1kh78jj1rr.cloudfront.net/thumbnail/', '').replace('_M', '').replace('_thumb', '').replace('?response-content-disposition', '');
}

Open in new window

Avatar of Joe Howard

ASKER

I need a regex, the system I use requires a regex input to do its processing.
For me this works:
/.*\/(.*?)_?[SMLt]?[humb]*\.jpg.*/

Open in new window


You can test and tweak and understand it @ https://regex101.com/
return psFileName.replaceFirst(".*/(.*?)(_(S|M|L|thumb))?\\.jpg.*", "$1");
Doesn't work for me, please see it live at http://www.regexr.com/3ch31
ozo doesn't work either http://www.regexr.com/3ch34
Avatar of Uros Gaber
Uros Gaber

try:
^.*\/(.*(\_(S|M|L|thumb))?\.jpg).*$

Open in new window

please note that the regexr site shows only first match from the whole string, so if you paste all of the URLs in the regexr only the first will match, you can delete one by one and see if the next one matches - as you would also process each line by line
If you add the flag global you have 9 matches
/https://.*/(.*)_/m

Open in new window

HTH,
Dan
with my regex if you go with global and multiline flags all lines match
http://www.regexr.com/3ch37
This makes me think my solution works: https://regex101.com/r/zL8dS5/1
Errata:
/https://.*/(.*)_(M|L|thumb)*.*\./g

Open in new window

Hi,

pls try

.*\/(.*?)(_([SML]|thumb))?\.jpg.*

Open in new window

Regards
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great, thanks.
Thanx 4 axxepting