0

I have a requirement to validate if an Account name has greek or english characters in the same Word, but not in the whole field. For example, i don't want to throw an exception if account name is "Γιώργος George". This String Contains of two words, a greek one and an English one and it is valid. However, i want to throw Exception when one word consists of Characters from both languages. For example, this Account name should be invalid: "Γιώrgos". This word has three first letters Greek and the rest ones English. I am thinking that i need to identify the whole string, then break it into pieces by identifying the "Space" character meaning it's a different word.. And then making the appropriate checks... But i don't know how to do it, especially how to identify the Space... Any ideas??? Here is what i have done until now but it is not correct since it throws exception if in the whole String there are greeks and English.

public static void validations(Account acc, boolean invokedUI) {
if(acc.ea_FirstName__c != null && acc.ea_FirstName__c != ''){
                String FNameRegexEng = '[ A-Za-z- . , _  / % ( ) \\\\ &]*'; //For english characters
                String FNameRegexGr = '[ Α-Ωα-ω- ίΊϊΐόΌάΆέΈύΎϋΰήΉώ . , _  / % ( ) \\\\ &]*'; //For Greek characters
                Pattern MyPatternEng = Pattern.compile(FNameRegexEng);
                Matcher MyMatcherEng = MyPatternEng.matcher(acc.ea_FirstName__c);
                Boolean resultEng = MyMatcherEng.matches();

                Pattern MyPatternGr = Pattern.compile(FNameRegexGr);
                Matcher MyMatcherGr = MyPatternGr.matcher(acc.ea_FirstName__c);
                Boolean resultGr = MyMatcherGr.matches();

                if(acc.ea_FirstName__c != null){
                    if (((!(resultGr))||(resultEng)==true)&&((!(resultEng))||(resultGr)==true)) {                                                                                                             
                        results.outputMessage= 'Name should contain only greek or english characters';
                        throw new validException(results.outputMessage);

                    }
                }
            }
2

You don't need the entire "split and search"; regular expressions can do this all in a single pass. Here's an example that accepts only Latin groups and the Greek groups:

Pattern p = Pattern.compile('(\\p{IsGreek}+|\\p{IsLatin}+)(\\s+(\\p{IsGreek}+|\\p{IsLatin}+))*');

This will match words that are all English, or all Greek, but not other combinations.

It works by stating that each capture group (parentheses) may contain only one set or the other:

(\p{IsGreek}+|\p{IsLatin}+)

And that there may be zero or more additional groups separated by spaces:

(\s+(\\p{IsGreek}+|\p{IsLatin}+))*

public static void validations(Account acc, boolean invokedUI) {
    if(acc.ea_FirstName__c != null) {
        Pattern p = Pattern.compile('(\\p{IsGreek}+|\\p{IsLatin}+)(\\s+(\\p{IsGreek}+|\\p{IsLatin}+))*');
        if(!p.matches(acc.ea_FirstName__c)) {
            results.outputMessage= 'Name should contain only greek or english characters';
            throw new validException(results.outputMessage);
        }
    }
}
1
  • I will check it out and let you know:) Anyhow, i would really like to thank you for one more time, it's not the first time you help me! Thanks! Oct 31 '19 at 21: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.