Öffentliche Dateiansicht: Raw-Dateien, Tree, Releases und Issues sind ohne Login verfügbar.
internal/detector/phone.go Raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package detector

import (
	"regexp"
	"strings"
)

var phoneRE = regexp.MustCompile(
	`(?:(?:\+|00)(?:49|43|41)[\s()\-]*(?:\(0\)[\s()\-]*)?\d[\d\s()\-]{5,16}\d|0[1-9]\d[\d\s\-/]{5,13}\d)`,
)

func detectPhone(text string) []Finding {
	var out []Finding
	for _, loc := range phoneRE.FindAllStringIndex(text, -1) {
		raw := strings.TrimRight(text[loc[0]:loc[1]], " \t")
		digits := 0
		for _, c := range raw {
			if c >= '0' && c <= '9' {
				digits++
			}
		}
		if digits < 9 {
			continue
		}
		if loc[0] > 0 {
			prev := text[loc[0]-1]
			if prev >= '0' && prev <= '9' || prev == '+' {
				continue
			}
		}
		end := loc[0] + len(raw)
		if end < len(text) && text[end] >= '0' && text[end] <= '9' {
			continue
		}
		out = append(out, Finding{
			Type: PiiPhone, Start: loc[0], End: end,
			Text: raw, Confidence: 1.0,
		})
	}
	return out
}