Activity Forums Salesforce® Discussions Is there any way to validate UTF-8 in regex?

  • Is there any way to validate UTF-8 in regex?

    Posted by Nitish on April 14, 2016 at 9:20 am

    I have basic validation rules setup for name fields:

    NOT(REGEX(FirstName, "^[A-Za-z\\. '-]+$"))

    Want to allow only letters, periods, spaces, hyphens and apostrophes in the name field. The problem with this is that it does not allow accented characters (graphemes). I've tried some simplified ideas based on a regex tutorial and the Java Docs Salesforce links to, but they do not work:

    NOT( REGEX( FirstName , "\\P{M}\\p{M}") )
    NOT( REGEX( FirstName , "\\p{Alpha}") )
    NOT( REGEX( FirstName , "\\X") )
    Has anybody else run into this problem? How do you validate names with accent marks?

    shariq replied 6 years, 1 month ago 4 Members · 3 Replies
  • 3 Replies
  • shafali

    Member
    April 14, 2016 at 9:26 am

    This might need some refinement, but my understanding is \p{L} will match "a single code point in the category 'letter'". Try this:

    String FirstName = 'Fredé';

    Pattern regexPattern = Pattern.compile('^[\\p{L}\\. \'-]+$');
    Matcher regexMatcher = regexPattern.matcher(FirstName);

    if (!regexMatcher.matches()) {
    System.debug(LoggingLevel.Warn, 'No Matches');
    } else {
    System.debug(LoggingLevel.Debug, 'Matches');
    }

    According to the Regex Tutorial: Unicode Character Properties you will probably need to add \p{M}*to optionally match any diacritics:

  • Parul

    Member
    September 20, 2018 at 12:53 am

    Hi,

    String FirstName = 'Parul';

    Pattern regexPattern = Pattern.compile('^[\\p{L}\\. \'-]+$');
    Matcher regexMatcher = regexPattern.matcher(FirstName);

    if (!regexMatcher.matches()) {
    System.debug(LoggingLevel.Warn, 'No Matches');
    } else {
    System.debug(LoggingLevel.Debug, 'Matches');
    }

  • shariq

    Member
    September 20, 2018 at 7:34 pm

    Hi,

    Yes you can do it through regex.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos