Question

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
Was it helpful?

Solution

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

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];
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top