-1

So I have a string like this (the hashtags are delimiters)

A1###B2###C3###12345.jpg

I was wondering how would I access A1, B2 and C3

STRING1###STRING2###STRING3###STRING4.jpg
SOME###THING###HERE###MEH.jpg
EXTRACT###THIS###PLEASE###pah.jpg

In one instance I'd like to extract the first string. In another the second, in another the third. I will be using this with Adobe Bridge to extract metadata items from the filename

I am looping through each filename so would need

Var1 = FirstString
Var2 = SecondString
Var3 = ThirdString
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
pee2pee
  • 3,619
  • 7
  • 52
  • 133

2 Answers2

2
[^#]+(?=###)

will match all substrings in your strings that are followed by ###

>>> s = "STRING1###STRING2###STRING3###STRING4.jpg"
>>> import re
>>> re.findall("[^#]+(?=###)", s)
['STRING1', 'STRING2', 'STRING3']

Or, for the example in your comment:

>>> s = "Slayer###Reading Festival###James###123.jpg"
>>> artist, event, photographer = re.findall("[^#]+(?=###)", s)
>>> artist
'Slayer'
>>> event
'Reading Festival'
>>> photographer
'James'

Assuming that Adobe Bridge has an ECMAScript-based scripting engine, you can use a different regex:

var myregexp = /^([^#]+)###([^#]+)###([^#]+)###/;
var match = myregexp.exec(subject);
if (match != null) {
    artist = match[1];
    event = match[2];
    photographer = match[3];
}
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • So let's say I have Slayer###Reading Festival###James###123.jpg. How would I assign Slayer to the variable artist, Reading Festival to event and James to Photographer? – pee2pee Sep 01 '12 at 15:31
  • @JanuszJasinski: See my edit. (The language used is Python; you didn't specify which language you would be using, so I picked the one I know best.) – Tim Pietzcker Sep 01 '12 at 15:33
  • 1
    I am using it within Adobe Bridge JavaScript (JSX) – pee2pee Sep 01 '12 at 15:36
  • @JanuszJasinski: OK, I've added an ECMAScript-based answer. I would expect that to work in JSX, too. – Tim Pietzcker Sep 01 '12 at 15:40
-1

This would be your regular expression:

(A1).*(B2).*(C3).*\.jpg

This will capture the three parts you want, while ignoring the rest of the string.

To access the parts, you just use \1,\2\,\3 respectively.

hbhakhra
  • 4,206
  • 2
  • 22
  • 36
  • I should be more clear STRING1###STRING2###STRING3###STRING4.jpg In one instance I'd like to extract the first string. In another the second, in another the third. I will be using this with Adobe Bridge to extract metadata items from the filename – pee2pee Sep 01 '12 at 15:27
  • No, this is wrong. You can only use things like `\1`, `\2`, and `\3` within the very same pattern, not outside of it, and even then only in certain flavors of pattern-matching languages. – tchrist Sep 01 '12 at 15:33