この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Pattern と Matcher の例

Matcher クラスの end メソッドは、最後の文字が一致した後の一致文字列の位置を返します。これは、文字列を解析中に一致する部分が見つかった後、次の一致を見つけるなど別の処理を行う場合に使用します。

正規表現構文では、? は 1 つ一致、または一致がないことを示し、+ は 1 つ以上一致することを示します。

次の例では、Matcher オブジェクトと共に渡された文字列がパターンに一致します。これは、(a(b)?) が、文字列 'ab' ('a' の後に 'b' が 1 回) に一致するためです。次に、最後の 'a' ('a' の後に 'b' が 1 つもない) に一致します。

1pattern myPattern = pattern.compile('(a(b)?)+'); 
2matcher myMatcher = myPattern.matcher('aba');
3System.assert(myMatcher.matches() && myMatcher.hitEnd());
4
5// We have two groups: group 0 is always the whole pattern, and group 1 contains 
6// the substring that most recently matched--in this case, 'a'. 
7// So the following is true:
8
9System.assert(myMatcher.groupCount() == 2 &&
10              myMatcher.group(0) == 'aba' && 
11              myMatcher.group(1) == 'a');
12 
13// Since group 0 refers to the whole pattern, the following is true:
14
15System.assert(myMatcher.end() == myMatcher.end(0));
16
17// Since the offset after the last character matched is returned by end, 
18// and since both groups used the last input letter, that offset is 3
19// Remember the offset starts its count at 0. So the following is also true:
20
21System.assert(myMatcher.end() == 3 && 
22              myMatcher.end(0) == 3 && 
23              myMatcher.end(1) == 3);

次の例では、メールアドレスが正規化され、類似するメールアドレスに対して異なる最上位のメイン名やサブドメインがある場合、重複が報告されます。たとえば、john@fairway.smithcojohn@smithco に正規化されます。

1class normalizeEmailAddresses{
2
3    public void hasDuplicatesByDomain(Lead[] leads) {
4           // This pattern reduces the email address to 'john@smithco' 
5           // from 'john@*.smithco.com' or 'john@smithco.*'
6        Pattern emailPattern = Pattern.compile('(?<=@)((?![\\w]+\\.[\\w]+$)
7                                               [\\w]+\\.)|(\\.[\\w]+$)');
8           // Define a set for emailkey to lead:
9        Map<String,Lead> leadMap = new Map<String,Lead>();
10                for(Lead lead:leads) {
11                    // Ignore leads with a null email
12                    if(lead.Email != null) {
13                           // Generate the key using the regular expression
14                       String emailKey = emailPattern.matcher(lead.Email).replaceAll('');
15                           // Look for duplicates in the batch
16                       if(leadMap.containsKey(emailKey)) 
17                            lead.email.addError('Duplicate found in batch');
18                       else {
19                           // Keep the key in the duplicate key custom field
20                            lead.Duplicate_Key__c = emailKey;
21                            leadMap.put(emailKey, lead);
22                       }
23                 }
24             }
25                // Now search the database looking for duplicates 
26                for(Lead[] leadsCheck:[SELECT Id, duplicate_key__c FROM Lead WHERE 
27                duplicate_key__c IN :leadMap.keySet()]) {
28               for(Lead lead:leadsCheck) {
29               // If there's a duplicate, add the error.
30                   if(leadMap.containsKey(lead.Duplicate_Key__c)) 
31                      leadMap.get(lead.Duplicate_Key__c).email.addError('Duplicate found 
32                         in salesforce(Id: ' + lead.Id + ')');
33            }
34        }
35    }
36 }