Using Regex to Split String

1k views Asked by At

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
2

There are 2 answers

4
Tim Pietzcker On BEST ANSWER
[^#]+(?=###)

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];
}
2
hbhakhra On

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.