Using REGEX to pull the info I want out of the following strings?

Hello,

I am working on a website that has product codes with the format t1234_blu where the first character is always a letter and the next 4 are numbers. There is then alway three letters after the underscore.

I have been trying, and failing to come up with a REGEX to pull product codes out of the following strings:

http://www.mywebsite.com/productimages/productlarge/19wspr_w0297_nav_w03.jpg

http://www.mywebsite.com/productimages/productlarge/19wspr_t8793_drd.jpg

/content/images/gifs/19xmss_g0305_grn.gif

/content/images/gifs/19xmss_b4877_blu.gif

Could someone please show me how this would be done is the most robust way possible?

Any help is much appreciated.

Thanks!

Based on your description:

/[a-z]\d{3}_[a-z]{3}/gi

You can easily test and interpret regexes with a tool like https://regex101.com/

PS: the example you gave has 4 digits after the letter, not 3.

A few slight modifications I’d make to ArtemPetrov’s regex:

/^[a-z]\d{4}_[a-z]{3}$/

1 Like