0

I have a string with potentially 9 values delimited by colons. I will have 2 potential structures for that string:

value1:value2:value3:value4:value5::value7:value8:
value1:value2:value3:value4::value6:value7::value9

I need to be able to break this down and store each individual value. We've come up with something like this

^(.+)?\:(.+)?\:(.+)?\:(.+)?\:(.+)?(\:\:)(.+)?\:(.+)?(\:\:)$

but it isn't allowing for both structures.

This is being used in Adobe Analytics to capture the individual values if that helps at all.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
csitty
  • 11
  • 2
  • Try [`^([^:\n]*):([^:\n]*):([^:\n]*):([^:\n]*):([^:\n]*):([^:\n]*):([^:\n]*):([^:\n]*):([^:\n]*)$`](https://regex101.com/r/CRnys1/1). You may remove `\n` if you are not running it on a multiline content. – Wiktor Stribiżew Oct 09 '17 at 19:38
  • Are fewer than 9 values permitted? That is, is `value1:value2`, for example, a valid string? – David Faber Oct 09 '17 at 21:13

1 Answers1

0

I assume this is for Classification Rule Builder(CRB). @Wiktor's commented answer is okay, but you shouldn't need to include the \n in the regex he provided. One thing his regex assumes though is that all of your existing keys have have all 9 values or at least placeholder colons for them. In my experience in practice this isn't always the case; historical data may have less because requirements were changed. If this is the case for you, then here is a slightly modified regex that @Wiktor provided:

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

This will give you $1 through $9 for the values:

value1:value2:value3:value4:value5:value6:value7:value8:value9

$1 = value1
$2 = value2
$3 = value3
$4 = value4
$5 = value5
$6 = value6
$7 = value7
$8 = value8
$9 = value9

Or you can have empty :: placeholders for them, e.g.

value1:value2::value4:value5::value7:value8:value9

$1 = value1
$2 = value2
$3 = 
$4 = value4
$5 = value5
$6 = 
$7 = value7
$8 = value8
$9 = value9

And it will also match the present values if you don't have the full 9 value string, e.g.

value1:value2::value4

$1 = value1
$2 = value2
$3 = 
$4 = value4
Crayon Violent
  • 30,524
  • 3
  • 51
  • 74