1

I have a string that could contain any of these values set @string = "a, b, c"

I thought I could do this using AMPscript

if IndexOf(@string,'a') > 0 or IndexOf(@string,'b') > 0 or IndexOf(@string,'c') > 0 then 

   /* do nothing */

else

  RaiseError('Unknown value submitted',true)

endif

The idea being if my string contains, say, "a, b, c, x", or anything other than a or b or c there would be an error and skip that record.

But it's not working and I have no idea why. Anyone have ideas?

1
  • It is possible indexOf is 1 based in AMPscript - I don't know - but in other cases such a function is usually 0 based, so a value is found when indexOf(value) >= 0.
    – Phil W
    Sep 11, 2020 at 22:11

1 Answer 1

2

You could use the regexmatch function:

Code

%%[

set @pattern = "[^(a|b|c|,|\s)]+"
set @str = "a, b, c, x"
set @result = RegExMatch(@str, @pattern, 0)
output(concat("<br>result: ", @result))

]%%

Output

result: x

In your example:

%%[

set @pattern = "[^(a|b|c|,|\s)]+"
set @str = "a, b, c, x"
set @result = RegExMatch(@str, @pattern, 0)

if empty(@result) then 

   /* do nothing */

else

  RaiseError('Unknown value submitted',true)

endif

]%%

I used this to test the pattern.

1
  • Thank you! That worked. Now what if in my pattern I wanted to use a word boundary, say @str = "dog, cat, bird"? I know I need to use a word boundary but I dont know where \b belongs: set @pattern = "[^(\bdog\b|\bcat\b|\bbird\b|,|\s)]+"?
    – Kyle
    Sep 12, 2020 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.