Inspecionar uma string com uma substring de dicionário de exclusão

Omita correspondências de verificação que incluam a substring "TEST"

Mais informações

Para ver a documentação detalhada que inclui este exemplo de código, consulte:

Exemplo de código


Para saber como instalar e usar a biblioteca de cliente do Cloud DLP, consulte Bibliotecas de cliente do Cloud DLP.

using System;
using System.Collections.Generic;
using System.Linq;
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Dlp.V2;

public class InspectStringWithExclusionDictSubstring
    public static InspectContentResponse Inspect(string projectId, string textToInspect, List<String> excludedSubstringList)
        var dlp = DlpServiceClient.Create();

        var byteItem = new ByteContentItem
            Type = ByteContentItem.Types.BytesType.TextUtf8,
            Data = Google.Protobuf.ByteString.CopyFromUtf8(textToInspect)

        var contentItem = new ContentItem { ByteItem = byteItem };

        var infoTypes = new string[]
        }.Select(it => new InfoType { Name = it });

        var exclusionRule = new ExclusionRule
            MatchingType = MatchingType.PartialMatch,
            Dictionary = new CustomInfoType.Types.Dictionary
                WordList = new CustomInfoType.Types.Dictionary.Types.WordList
                    Words = { excludedSubstringList }

        var ruleSet = new InspectionRuleSet
            InfoTypes = { infoTypes },
            Rules = { new InspectionRule { ExclusionRule = exclusionRule } }

        var config = new InspectConfig
            InfoTypes = { infoTypes },
            IncludeQuote = true,
            RuleSet = { ruleSet }

        var request = new InspectContentRequest
            Parent = new LocationName(projectId, "global").ToString(),
            Item = contentItem,
            InspectConfig = config

        var response = dlp.InspectContent(request);

        Console.WriteLine($"Findings: {response.Result.Findings.Count}");
        foreach (var f in response.Result.Findings)
            Console.WriteLine("\tQuote: " + f.Quote);
            Console.WriteLine("\tInfo type: " + f.InfoType.Name);
            Console.WriteLine("\tLikelihood: " + f.Likelihood);

        return response;


Para saber como instalar e usar a biblioteca de cliente no Cloud DLP, consulte Bibliotecas de cliente do Cloud DLP.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class InspectStringWithExclusionDictSubstring {

  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String textToInspect = "Some email addresses:,";
    List<String> excludedSubstringList = Arrays.asList("TEST");
    inspectStringWithExclusionDictSubstring(projectId, textToInspect, excludedSubstringList);

  // Inspects the provided text, avoiding matches specified in the exclusion list.
  public static void inspectStringWithExclusionDictSubstring(
      String projectId, String textToInspect, List<String> excludedSubstringList)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (DlpServiceClient dlp = DlpServiceClient.create()) {
      // Specify the type and content to be inspected.
      ByteContentItem byteItem =
      ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();

      // Specify the type of info the inspection will look for.
      // See for complete list of info types.
      List<InfoType> infoTypes = new ArrayList<>();
      for (String typeName :
          new String[] {"EMAIL_ADDRESS", "DOMAIN_NAME", "PHONE_NUMBER", "PERSON_NAME"}) {

      // Exclude partial matches from the specified excludedSubstringList.
      ExclusionRule exclusionRule =

      // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype.
      InspectionRuleSet ruleSet =

      // Construct the configuration for the Inspect request, including the ruleset.
      InspectConfig config =

      // Construct the Inspect request to be sent by the client.
      InspectContentRequest request =
              .setParent(LocationName.of(projectId, "global").toString())

      // Use the client to send the API request.
      InspectContentResponse response = dlp.inspectContent(request);

      // Parse the response and process results
      System.out.println("Findings: " + response.getResult().getFindingsCount());
      for (Finding f : response.getResult().getFindingsList()) {
        System.out.println("\tQuote: " + f.getQuote());
        System.out.println("\tInfo type: " + f.getInfoType().getName());
        System.out.println("\tLikelihood: " + f.getLikelihood());


Para saber como instalar e usar a biblioteca de cliente do Cloud DLP, consulte Bibliotecas de cliente do Cloud DLP.

def inspect_string_with_exclusion_dict_substring(
    project, content_string, exclusion_list=["TEST"]
    """Inspects the provided text, avoiding matches that contain excluded tokens

    Uses the Data Loss Prevention API to omit matches if they include tokens
    in the specified exclusion list.

        project: The Google Cloud project id to use as a parent resource.
        content_string: The string to inspect.
        exclusion_list: The list of strings to ignore partial matches on

        None; the response from the API is printed to the terminal.

    # Import the client library.

    # Instantiate a client.
    dlp =

    # Construct a list of infoTypes for DLP to locate in `content_string`. See
    # for more information
    # about supported infoTypes.
    info_types_to_locate = [{"name": "EMAIL_ADDRESS"}, {"name": "DOMAIN_NAME"}]

    # Construct a rule set that will only match if the match text does not
    # contains tokens from the exclusion list.
    rule_set = [
            "info_types": info_types_to_locate,
            "rules": [
                    "exclusion_rule": {
                        "dictionary": {"word_list": {"words": exclusion_list}},

    # Construct the configuration dictionary
    inspect_config = {
        "info_types": info_types_to_locate,
        "rule_set": rule_set,
        "include_quote": True,

    # Construct the `item`.
    item = {"value": content_string}

    # Convert the project id into a full resource id.
    parent = f"projects/{project}"

    # Call the API.
    response = dlp.inspect_content(
        request={"parent": parent, "inspect_config": inspect_config, "item": item}

    # Print out the results.
    if response.result.findings:
        for finding in response.result.findings:
            print(f"Quote: {finding.quote}")
            print(f"Info type: {}")
            print(f"Likelihood: {finding.likelihood}")
        print("No findings.")

A seguir

Para pesquisar e filtrar amostras de código de outros produtos do Google Cloud, consulte a Amostra do navegador do Google Cloud.