Separate string into separate variables?

If I had a variable that included these strings how could I exclude the comma and save the strings as separate variables?

A5011111, A5022222, A1102822

so that I end up with

Variable a: A5011111
Variable b: A5022222
Variable c: A1102822

Sometime I can have 2,3,4,5 refs in a single variable that I want to separate

Are the references of uniform length? Are they all alphanumeric? Are they always comma delimited? This is not my forte but knowing this would expedite an effective answer from someone else :slight_smile:

1 Like

First, remove the spaces on either side of the commas:

If there might be spaces at the start or end of the line, get rid of them too.

Then you need to search for the comma separated elements ([^,]*), - a sequence of zero or more not-commans, captured, followed by a space. to make a section optional, you wrap it in a non-capturing sequence and add a question mark (?:optional)?. The result for 2-6 elements would be:

([^,]*),([^,]*)(?:,([^,]*)(?:,([^,]*)(?:,([^,]*)(?:,([^,]*))?)?)?)?

Which looks very complicated, but really is not, it is just the two elements above combined and nested.

([^,]*),([^,]*)
   (?:
      ,([^,]*)
      (?:
         ,([^,]*)
         (?:
            ,([^,]*)
            (?:
               ,([^,]*)
            )?
         )?
      )?
   )?

Add an up arrow and a $ to match the whole line and you're good to go.

You could make the expression deal with the spaces as well, by adding \s* to the start and end and either side of every comma, and making the the pattern non-greedy ([^,]*?). But often with complex regular expressions it is simply easier to whittle, solving each part in turn, instead of trying to deal with a complex input.

3 Likes

Here is a macro that should work for you.
It will parse one line of one or more comma separated values (CSV) into individual KM Variables.

1 Like

If my variable held the following info and I want to extract the year month and day and save them as separate variables how would I do this

Variable 1: 1952-02-22
Variable 2: 1952/02/22

I would want to separate by - or by /

ok I got it I just changed the comma :slight_smile: in your template macro above.

Many thanks again.