Skip to main content

#REGEX0 discussing

The following REGEX works in https://regex101.com/ but not in Salesforce.

^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})?\z

Source: https://stackoverflow.com/questions/15920008/regex-for-bic-check

 

It checks whether a given BIC :

  • has 8 or 11 characters, of which
  • first 6 must be alphabetic (4 bank code + 2 country code)
  • next 2 must be alphanumeric (location code)
  • last 3 are optional, alphanumeric (branch code)

Examples

Correct: AGRIFRPP882

Correct: NOLADE21STS

Correct: OPSKATWW

Incorrect: OPSKATW

Incorrect: AABB12CC

 

When using REGEX in Salesforce, backslash characters \ must be changed to double backslashes \\ because backslash is an escape character in Salesforce. The expression must also be put within quotation marks. See Salesforce help.

 

Thus the validation rule would be: 

NOT(REGEX(BIC__c, "^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})?\\z"))

 

However, when checking on AGRIFRPP882, which is a correct BIC, the validation rule always fires.

 

  #REGEX #Validation Rule

2 answers
  1. Sep 2, 2021, 8:03 PM

    Am I bad... the formula allowed only for lower case.

     

    This one works (provided you have a data input flow that cleans the data and puts it into UPPERCASE before validation):

     

    NOT(REGEX(BIC__c, "^[A-Z]{6}[0-9A-Z]{2}([0-9A-Z]{3})?\\z"))
0/9000

I usually research and tweak examples I find to fit my needs, but in this case I'm struggling to understand how the REGEX function can be used to complete the task of disallowing any values outside of numbers, parentheses or hyphens in a phone number field.

 

I want users to be able to use parentheses or hyphens or "+" but not be required to. I also want users to enter numbers, but not specific amount of digits. I just don't want letters to be allowed.

 

Here's what I have so far: 

 

AND( NOT(ISBLANK(Phone)), (REGEX( Phone ,"^[0-9 + ( ) -]*$")) )

 

But it's throwing up errors when I enter "1234567" or "123-4567". What am I doing wrong? 

 

#Validation Rule #Formulas

5 answers
  1. Tom Bassett (Vera Solutions) Forum Ambassador
    Oct 14, 2021, 4:44 PM
    Have taken this from here https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000A8TY1SAN

    REGEX(phone__c, "[0-9\(\)-]*")

0/9000

Hello,  I have the following text:   Trigger to search for particular words in a string

 I have the 'WOT Order' in this string. What I want to achieve is the following:  I created 2 text fields:    

 

1. The Task has been already completed

2. User not allowed to complete the task in future date

 

Now, I want to store in these fields the WOT Order corresponding to each error message.  

 

First Example: In the first row we have WOT Order :0010 following by both error messages; (1)Task has been already completed(2)If the user is authorized,then he can set completed date as future date.  

 

I want to populate in this case:  

 

1. The Task has been already completed: 0010

2. User not allowed to complete the task in future: 0010.  

 

Second Example: For the next record I have WOT Order :0020 but its only for this error message:  If the user is authorized,then he can set completed date as future date.  

 

I want to populate in this case:  

 

1. The Task has been already completed: 

2.  User not allowed to complete the task in future date: 0020   

 

And so on... How can I accomplish this? Do I need to use regex?

 

#Apex Trigger  #Apex  #REGEX  #Sales Cloud  #Service Cloud  #Automation

7 answers
  1. Sep 16, 2021, 4:59 PM

    I've managed to do it ! :)   

    String recordDescription = formRecord.SVMX_PS_Description__c

    String wordToFind = 'WOT Order :';

    Pattern word = Pattern.compile(wordToFind);

    Matcher match = word.matcher(formDescription);

    while (match.find()) {

    substring = formDescription.mid(match.start(), 50);

    System.debug('substring '+ substring );

    if(substring.contains('If the user is')){

    String newString = formDescription.mid(match.start()+12, 7);

    listOfWOTwithFirstValidation.add(newString);

    formRecord.WOT_Order_Future_date_not_allowed__c = String.Join(new List<String>(listOfWOTwithFirstValidation), '');

    }

    if(substring.contains('The Task has be')){

    String newString = formDescription.mid(match.start()+12, 7);

    listOfWOTwithSecondValidation.add(newString);

    formRecord.WOT_Order_Task_Already_Completed__c = String.Join(new List<String>(listOfWOTwithSecondValidation), '');

    }

    }

0/9000

Hi all,

I am trying to build a validation rule with REGEX for the field Phone. The validation rule should check the following:

  • Allow: Numbers
  • Allow: Characters such as '+', spaces, parenthesis '(', ')', '/', '-'
  • Allow: Character '+' should be only at the beginning of the phone number not in the middle or at the end

So, just searching pages with REGEX tutorials but unfortunately I could not find any appropriate in order to also decrypt the following expression literally:

 

NOT(REGEX(Phone,"(\\D?[0-9]{3}\\D?)[\\s][0-9]{3}-[0-9]{4}

")

)

 

No idea what do the following do?:

 

  • \\D? (at the beginning and at the end)
  • {3}
  • \\s
  • {4}

I would appreciate if experts in REGEX could give me some guidance here. 

 

#REGEX  #Salesforce Developer  #Sales Cloud  #Service Cloud  #Nonprofit  #Automation  #Integration

34 answers
  1. Sep 14, 2021, 1:36 PM

    Hi Laura,

     

    I guess I'll join the free-for-all to get best answer on this thread :)

     

    First of all, here is a regex that might be what you need:

    NOT(REGEX(Phone, "[+]{0,1}[-\\s\\.\\/0-9()]+"))

     

    Now I'll explain what the various parts of it do:

    [+]{0,1} means the character + can appear between 0 and 1 times. Because it's in the beginning of this expression, this character is only accepted at the beginning of the text we're matching.

     

    [-\\s\\.\\/0-9()]+ means that any of the characters inside the square brackets (will detail in a bit) are allowed any number of times (but must appear at least once). The + just after the closing bracket means "at least once".

     

    Now as for the specific characters here:

    - means the literal hyphen character

    \s means a whitespace character

    \. means a literal dot/period

    \/ means the literal character slash

    0-9 means any digit between 0 and 9

    () means closing or opening brackets

     

    Oh, and to cover the elements in your original regex, I can explain those as well:

    \D means any digit (same as 0-9)

    {3} and {4} mean that the preceding block has to appear exactly 3 or 4 times, respectively.

     

    You will notice that in this regex we actually use a double backslash \\ instead of a single one. This is actually not part of the Regex - it's for Salesforce to understand we mean a literal backslash and not a special character (like a tab \t or a line break \n). So we're actually double-encoding the expression, once for Salesforce, and a second time for the Regex engine.

     

    One of my favourite tools for writing and examining regular expressions is Regex101. You can try this one here. You can hover over the various elements of the expression to see what they mean, and see the results in real time.

0/9000

Hi All,

 

I need to enforce the structure of mobile phone number fields, I am almost there but cant figure out how to allow for a special character at the start.

 

Example Accepted Formats

+61 402 123 123 || 0402 123 123

 

Rule so Far

AND( NOT(MobilePhone ="") , LEN(MobilePhone) > 0 , NOT(REGEX( MobilePhone ,"(^614[0-9]{2}(| )[0-9]{3}(| )[0-9]{3}$)|(^04[0-9]{2}(| )[0-9]{3}(| )[0-9]{3}$)")))

 

Any help would be greatly appreciated.

 

Thanks,

Brenden

 

@Formulas - Help, Tips and Tricks #REGEX #Validation Formula

3 answers
  1. Aug 31, 2021, 7:29 AM

    Hi  Brenden ,

     

     Try this

     

    AND( 

    NOT(MobilePhone ="") ,

    LEN(MobilePhone) > 0 , 

    NOT(REGEX( MobilePhone ,"(^ [+] 614[0-9]{2}(| )[0-9]{3}(| )[0-9]{3}$)|(^04[0-9]{2}(| )[0-9]{3}(| )[0-9]{3}$)"))

0/9000

Can someone give me a to use in an input field in the flow that does not allow the user to put a different value from this format x.xx 

In this case it is for the dollar rate

 

#Flow #Flow Builder #Apex Class #REGEX #Validation Formula

4 answers
0/9000

I want to allow these special characters: & ' * @ \ | ^ : , $ = ! / ` > - { ( [ < # % . + ? " } ) ] ; ~ _   and a blank space

 

Here is the validation I have but still getting Syntax error:

NOT(REGEX( Field Name ,"^[a-z A-Z&'*@\|^:,=!/`>{([<#%.+?"})]; ~_-]$")))

 

#REGEX #Validation Rule #Data Management

3 answers
  1. Jul 14, 2021, 5:31 PM

    Hi @Charles Porch III   

     

    REGEX are sometimes very clunky but it does work very well if it comes to validation. For example, in your case, every character has its own functionality but if we want to pass through that special character we need to pass with \ backslash.  

     

    Try to use this:

     

    NOT(REGEX(Field Name ,"^[a-zA-Z&'*@|^:,;~_\\-=!\\/`>{}()\\[\\]<#%.+?\"]$"))

     

    Note:  You may use character class “[^\w]” but again in your case you want to consider only specific characters to pass through.

     

    Thanks.

0/9000

I've built a validation rule in the past where special characters were not allowed at all...I copied it from somewhere.  Now I am trying to create a VR that restricts only specific special characters. I have read a few articles on creating regex but it's not clicking.  Is anyone able to help with this??

 

^:*~%<>={}/\[]

 

#Validation Rule #REGEX

6 answers
  1. Jul 12, 2021, 6:30 PM

    I tried updating to NOT(REGEX(Company,"^[a-zA-Z0-9^:*~%<>={}/\[]]*$")) and still get syntax error; wondering if it's because of the square brackets??

0/9000

Hello, I'm trying to create a validation rule on phone numbers that only allows numerical characters and standard symbols (i.e.: ()- ). I used the following in the Error Condition Formula:

REGEX(HomePhone, "[^

\\D-()\\s]

")

I didn't get any syntax errors, but after saving when I test it using disallowed characters I don't receive an error message. The rule is active. Anything I'm missing?

3 answers
0/9000

Flow Input Validation with REGEX gives syntax error

I'm trying to add validation to an Input field on a Flow Screen Element. The standard REGEX formula is ^A-Za-z0-9 \r\n@£$¥!\"#$%&'\(\)*\+,_.\/:;<=>?^{}\\\[~\]. In the Screen element, I have an input field called BlastBody, with Validation formula REGEX({!BlastBody},"[^A-Za-z0-9 \r\n@£$¥!\"#$%&'\(\)*\+,_.\/:;<=>?^{}\\\[~\]]"). When I save I get a Syntax error.

 

I read one post that says that we have to escape the \ of special characters, so I tried changing all the \ to \\ as recommended: REGEX({!BlastBody},"[^A-Za-z0-9 \\r\\n@£$¥!\\"#$%&'\\(\\)*\\+,_.\\/:;<=>?^{}\\\\\\[~\\]]") But that also gives me a syntax error.

 

Recommendations?

2 comments
0/9000