Welcome Guest

Pages: 1
Regex capturing more than one result at the same time
Gary-SwiftPostAugust 31, 2025, 04:39
Advanced
Posts: 70
Registered:
April 29, 2014, 06:12
Normal topicRegex capturing more than one result at the same time

I'm creating my own needlessly over-complicated sort action.

I have a question regarding the Regular Expression function.

Take the following string:

Sam Portable feat. Lcio Jack

This is in the Artist field and I want the Artist Sort field to become:

Portable, Sam feat. Jack, Lcio

So, both sides are sorted independently and the feat. remains in the middle.

I have done this. However, it takes 3 regex commands because I can only capture one part at at time.

Is there a way to assign 3 parts at the same time?

Something like:

^(.*?)\s(?i)(feat\.?|featuring)\s(.+)$

Captured:

$1 → Sam Portable

$2 → feat.

$3 → Lcio Jack

The assignments will be:

$1 -> Variable 1 | $2 -> Variable 2 | $3 -> Variable 3

This is a partial of my action.


    ' Handle feat/featuring Artist Names↵…
    ' Split the sort for like so: Portable feat. Lcio
    Return all matches for regular expression "(?i)\s(?:feat\.?|featuring)\s" in field Artist to field Variable 2
    Test if the Variable 2 field is not empty (Set test state)
    if true
        Return all matches for regular expression "^(.*?)(?=\s(?i)(?:feat\.?|featuring)\s)" in field Artist to field Variable 3
        Return all matches for regular expression "(?i)(?<=\s(?:feat\.?|featuring)\s).+$" in field Artist to field Variable 4
        # Prompt User Log "var 2 = ‘\v2’↵…"
        # Prompt User Log "var 2 = ‘\v2’↵…"
        Set named variable 'sortartist' to "\v3"
        Set named variable 'sortfeatartist' to "\v4"
        
        Set for last name, first name in named variable 'sortartist'
        Set for last name, first name in named variable 'sortfeatartist'
        
        Set the Sort Artist field to "\<sortartist>\v2\<sortfeatartist>"
        Set named variable 'is_sorted' to "1"
    endif

I'm also doing what looks like inefficient assignment with `Set named variable 'sortartist' to "\v3"`. Maybe there is a better way to do that.

2MR2PostAugust 31, 2025, 10:02
Avatar photo
Administrator
Posts: 2362
Registered:
August 23, 2012, 19:27
Normal topicRegex capturing more than one result at the same time

There's no way to get a regular expression to assign capture group results to separate destinations. You can work with returned match locations but in this case there's easier ways to do what you want.

If you're okay with standardizing the 'featuring' representation, you can do the following without using a regex:

1: Copy the Artist field to Variable 0
2: Replace first occurrence of "featuring|feat.", case insensitive, match words, OR matching in Variable 0 with "\&0000"
3: Split Variable 0 at delimiter "\&0000" save right portion to Variable 1
4: Set for last name, first name in Variable 0
5: if Variable 1 is not empty
6: Set for last name, first name in Variable 1
7: Append "·feat. \v1" to Variable 0
8: endif
9: Copy the Variable 0 field to the Sort Artist field

Statement 1 copies Artist to Variable 0 so that Artist is not changed by subsequent manipulations

Statement 2 looks for the first occurrence of case insensitive 'featuring' or 'feat.' on word boundaries and replaces the sequence with a binary 0. (You can replace it with anything that wouldn't normally be in the field)

Statement 3 splits the Variable 2 field at the binary 0. Variable 0 gets everything before and Variable 1 gets everything after. Variable 0 has trailing spaces removed and Variable 1 has leading spaces removed. If the binary 0 is not found, Variable 1 will be empty.

Statement 4 sorts the front end or the entire field.

Statement 5 is an If statement. It limits execution to those statements which had the featuring text.

Statement 6 sorts the tail end

Statement 7 modifies Variable 0 by appending space feat. space Variable 1
Note you can choose whatever you like other than feat.

Statement 8 ends the file limiting stared on statement 5.

Statement 9 sets the Sort Artist field.

As an aside I notice that you have disabled Prompt statements which were intended for debugging. The Debug statement is a better approach as it can be configured when displayed to display anything 🙂

Gary-SwiftPostAugust 31, 2025, 10:23
Advanced
Posts: 70
Registered:
April 29, 2014, 06:12
Normal topicRegex capturing more than one result at the same time

Great, I'll go through what you said.

I knew there would be a better way of doing this. I started doing this as a bit of a challenge with Regex.

I'll go back over it using your suggestions. Thanks for the very detailed response, I appreciate it.

Pages: 1
Mingle Forum by Cartpauj | Version: 1.1.0beta | Page loaded in: 0.031 seconds.