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

Newer Version Available

This content describes an older version of this product. View Latest

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 に正規化されます。

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