Forum Discussion
Need help with Regex
Hi team,
I want to include a set of users to an existing segment. This segment will be used for exclusions in all promotional campaigns. Let's call that segment as Level 3. The set of users are subscribers for a specific product. I know that I can't create a segment only for these set of users and include that segment in Level 3 as Level 3 is used in all marketing campaigns and Braze doesn't alllow me to do that. Therefore, one way to bell this cat is to use a "match regex" filter condition in Level 3 to identify that cohort that I want to exclude. The regex can target a custom attribute called "Active_Accounts". The "Active_Accounts" is a string and lists all the active account numbers for the user. The account numbers are comma separated and the product that I am targeting ( for the set of users) appears as the first value in "Active_Accounts". The account number for the set of users will follow this pattern : oabc-32HABHCPUV {10} or odefg-12AAAAAB {8} etc. But the account number always starts with a 'o' (lower caps). Need help to create a regex that I can match from the "Active_Accounts" so I can include this set of users in Level 3 using the match regex filter
- bobSpecialist
If there can only be a single instance of the account number, then you could keep it simple using
starts with 'o' and does not contain a comma.
If you need to more robustly match your account numbers, then you'd need to be more specific with the rules, but given what you've said already, something like this should work.
^o[a-z]{3}-[0-9A-Z]{8,10}$
Using the start and end of string characters (^ and $) to make sure there was only one instance.
If you had a list of things that should match and things that should not, I can probably better refine what you're looking for.
(and as David said, it's a starting point for a conversation.)- RajoriginSpecialist
Thank you very much, much appreciated bob , the reegx helps to identify the product in the string, which is great but the challenge when there are multiple products i.e. account numbers listed in the string. Don't think I can use the filter starts with 'o' as the target attribute account_active is a custom attribute and not within a nested object (in which case I could have used multi-criteria segmentation where account_active starts with 'o' AND matches regex ^o[a-z]{3}-[0-9A-Z]{8,10}$
Cheers
Raj
- bobSpecialist
Okay, ignore any simple starts-with/contains filters.
I thought you only wanted to match when there was a single identifier in the string, so no multiple values and no comma-separation?"...need only that user that has got only one account starting with "o" and they should not have any other account numbers."
In which case, the ^ at the start will ensure that the following character occurs at the beginning of the string and the $ at the end will ensure that there's nothing after the preceding matched characters - which in this case are specific enough to only match a single account number.
- DavidOStrategist II
Hey Rajorigin
I think I understand your need and my regex is not amazing but this might help get the conversation started.
o[a-z]{3,4}-\d{1,2}
Where 'o' is the lower case 'o', '[a-z]{3,4}' is looking for any sequence of 3 to 4 lowercase letters after the 'o' as it appears some users may have 3 and some may have 4. The '-' is just that, and the '\d{1, 2}' is looking for any one or two digit numbers on the end, as it looks like they could have 8, or 10 or probably other numbers.
So, you know, I built this by putting your question into CoPilot, which got me 95% there and then used Regex101 to test and debug what CoPilot gave me with your strings.
😊- RajoriginSpecialist
Hello David,
Thanks for the revert much appreciated. I tried similar regex before, and it worked. But in my specific used case I need only that user that has got only one account starting with "o" and they should not have any other account numbers. For example, the custom attribute will have values like A-123456, oxxx-AB12345689. I do not want this customer because they have another account starting with A. If there is a filter condition "does not matchregex" that would have been easier, but I can't see that.
Cheers/ Raj
- DavidOStrategist II
Hmm, yeah that's a trickier one for sure. I know you can use regex to reject or match certain values, positive and negative lookahead, but my knowledge of it would be limited at best. It might be worth investigating.
Related Content
- 7 months ago
- 2 years ago
- 2 months ago