Avatar of Joe Howard
Joe HowardFlag for United States of America asked on

regex needed

Regular ExpressionsJavaScript

Avatar of undefined
Last Comment
zzynx

8/22/2022 - Mon
Lee

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

ASKER
Joe Howard

I need a regex, the system I use requires a regex input to do its processing.
zzynx

For me this works:
/.*\/(.*?)_?[SMLt]?[humb]*\.jpg.*/

Open in new window


You can test and tweak and understand it @ https://regex101.com/
Your help has saved me hundreds of hours of internet surfing.
fblack61
ozo

return psFileName.replaceFirst(".*/(.*?)(_(S|M|L|thumb))?\\.jpg.*", "$1");
ASKER
Joe Howard

Doesn't work for me, please see it live at http://www.regexr.com/3ch31
ASKER
Joe Howard

ozo doesn't work either http://www.regexr.com/3ch34
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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
zzynx

If you add the flag global you have 9 matches
Dan Craciun

/https://.*/(.*)_/m

Open in new window

HTH,
Dan
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Uros Gaber

with my regex if you go with global and multiline flags all lines match
http://www.regexr.com/3ch37
zzynx

This makes me think my solution works: https://regex101.com/r/zL8dS5/1
Dan Craciun

Errata:
/https://.*/(.*)_(M|L|thumb)*.*\./g

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ozo

Rgonzo1971

Hi,

pls try

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

Open in new window

Regards
ASKER CERTIFIED SOLUTION
ozo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Joe Howard

Great, thanks.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
zzynx

Thanx 4 axxepting