4

I have to populate a shop's images and been provided with folders of images which arein the format, eg BRL0368 Side.jpg, 5510 Dura Guard Carpet.jpg.

Now what I want to do is chop all those down so I can just try and match up in excel the part numbers eg "BRL0368.jpg", "5510.jpg"

I have something called regex renamer which takes a regex match & a replacement value - and will recurse through a folder etc and batch rename.

Unfortunately at the moment I don't have much expereince with regex so it's a bit confusing for me - I tried many different options but not having much luck.

How do I say grab the first part (alpha & digit) up to the first space and dump the rest?

this is what I have been trying ([^\s]+) - I also tried \w etc.

Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
mro
  • 141
  • 2
  • 4
  • 11

2 Answers2

10

It may depend on the language used, but this should work :

/^[^\s]+/

To capture the first word and the last (the extension) you can use this expression :

/(^[^\s]+).*?(\.\w+)$/

Here's a javascript demo to clear things up : http://jsfiddle.net/eDB2z/1/

gion_13
  • 41,171
  • 10
  • 96
  • 108
  • Thanks for the demo, that's helpful on the regex tool page they have this kind of thing as a exmaple ;Match pattern: ^img00(\d+)_sshot(_thumbnail)? Replace pattern: tutorial$2.$1 – mro Apr 04 '12 at 09:22
1

This is what you need: \w*(?= )

Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
  • Hi - this seems to have some effect, only thing is it returns say;5510 dura guard carpet.jpg (returns = " Dura Gaurd Carpet.jpg") - what I'm after is say "5510.jpg" - I tried ^ at the beginning to match the first word - if I have that right ? – mro Apr 04 '12 at 09:15
  • In your question, you didn't say you need to leave the `.jpg` part – Ofer Zelig Apr 04 '12 at 09:24
  • BTW You can run my regex and programatically take first match only + file extension. – Ofer Zelig Apr 04 '12 at 09:26
  • Sorry about that - Interestingly - when I stick this in ^(\w*(?= )) and then put $1. in the replace pattern - it splits the words up but still doesn't get rid of the unmatched (5510. dura guard carpet.jpg) - It seems the match pattern is working then, just the replace / or the way I'm using the tool ? – mro Apr 04 '12 at 09:32
  • Thanks for all your help, this helped me undertsand regex a little more. – mro Apr 04 '12 at 09:38