Conditional regex

I’m trying to generate phone numbers using regex
if the id field is mobile the regex changes.
I’ve tried using a custom formula, but I get an error message

error: Access denied to method gen.

my code is:

if phone_number_id == ‘mobile’ then /324((7|8|9)1)\d{6}/.gen
else /32((1|2|3|4|5|6|7|8|9)1)\d{7}/.gen end

So now I’m trying to treat it as a string and replace the characters:
I put this in as regex : /32((1|2|3|4|5|6|7|8|9)1)\d{7}/ and then the following formula
if phone_number_id == "mobile" then this.to_s.insert(2,"4") this.to_s.insert(3,random(7,9).to_s).chop.to_i else this end

and this gets me the result I desired, but doesn’t resolve the original programming problem.

I’ve enabled generation using regular expressions in formulas, so the code in your first post should work now.

1 Like