0

I'm working on design patterns while I'm testing in the anonymous window it's working as expected. But when i trying to implement the same logic it's unable to find matches

My expression:'^([0-9])\\1*$|(?i)NA|\\[not provided\\]|^([_])\\1*$|[^\\w]*|(?i)[a-z]{5,}|(?i)[A-Z]{5,}|\\d{5,}|(?i)N/A|(?i)test|(?i)testing';

So, I'm trying to implement every single logic individually. My requirement is to find any field contains only special characters.

First Logic: [^\\w]*

I'm unable to understand why my logic is not working on an apex class. But working on the apex class. By the way, I'm using matches method

String emailId='Azuga Cavalry0';
Boolean result = false;
    Pattern EmailPattern = Pattern.compile(emailRegex);
    Matcher EmailMatcher = EmailPattern.matcher(emailId.trim());
    if(EmailMatcher.matches())
    {
        result=true;
        System.debug('Result details'+result);
    }

With the guidance of @sfdcfox i have found out the issues within my regular expression and updated the expression. One of my requirement is to find the fields which have only special characters.

My logic: \\p{Punct}+ And I'm using matches method only because I'm looking for a total field instead of specific words.

2

matches() matches the entire string against the pattern. What you're looking for is find(), which finds the first matching occurrence. Next, remember that * matches zero or more occurrences. You want at least one occurrence, which would be +. So, it should look like this:

String emailRegex = '[^\\w]+';
String emailId='Azuga Cavalry0';
Boolean result = false;
Pattern EmailPattern = Pattern.compile(emailRegex);
Matcher EmailMatcher = EmailPattern.matcher(emailId.trim());
if(EmailMatcher.find()) {
    result = true;
    System.debug('Result details'+result);
}

Rather than try to make a monster regex, which is difficult, consider a list of things you want to look for:

String[] patterns = new String[] {
  '^([0-9])\\1*$', '(?i)NA', ...
};
Boolean found = false;
for(String patt: patterns) {
  Pattern p = Pattern.compile(patt);
  Matcher m = p.matcher(emailString);
  found = m.find();
  if(found) {
    break;
  }
}

This will run slightly slower, but is often much easier than trying to build an entire string to match against.

It's also probably worth mentioning that simply validating for a valid email is probably easier than validating a select set of invalid emails, because you never know when someone might surprise you with something you didn't think of, resulting in a game of cat and mouse trying to keep out all the invalid values.

3
  • Sfdcfox but I'm looking only which fields having only special characters.but if I use find test___& will also consider as match
    – Pavan tej
    Jul 6 '20 at 18:21
  • @Pavantej You can still use ^ and $ to mark beginning/end boundaries of the entire string.
    – sfdcfox
    Jul 6 '20 at 18:23
  • Thank you so much for your guidance and thanks for sharing the knowledge.I have updated my question
    – Pavan tej
    Jul 7 '20 at 13:06

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.