FastPII Docs
Guides

Masking

Replace detected PII with asterisks preserving original span length.

Masking

Use mask() to replace each finding with asterisks (*) while preserving the original text span length. This reveals the format and length of PII without exposing the actual values.

Basic masking

from fastpii import FastPII, DEFAULT_PRIORITY
from fastpii.countries.cz import CzechPack

engine = FastPII(priority=DEFAULT_PRIORITY)
engine.register(CzechPack())

text = "Email: jan.novak@example.cz, RČ: 8001011238"
result = engine.mask(text)
print(result)

Output:

Email: *******************, RČ: **********

Using TransformationEngine directly

from fastpii.core.transform import TransformationEngine, MaskStrategy

result = engine.detect(text)
masked = TransformationEngine.apply(result, MaskStrategy())
print(masked)

How masking works

Masking uses the character span length (end - start) of each finding, not the length of the matched value. This means:

  • If a detector normalizes a value (e.g., strips formatting), the mask length matches the original text position
  • Overlapping findings are resolved by priority before masking

When to use masking

Masking is best when:

  • You need to preserve the visual structure of text (spacing, alignment)
  • Showing the approximate length of PII without revealing values
  • Creating partially anonymized views where format matters

On this page