# ReplaceRegExGroup Funzione per script e grafici

ReplaceRegExGroup() restituisce una stringa dopo aver sostituito una o più corrispondenze tra una stringa di input e il modello di espressione regolare composita specificato. Il testo che sostituisce il testo corrispondente è specificato nell'argomento to\_str. La funzione viene applicata da sinistra verso destra, ma se si specifica un valore occurrence negativo, la funzione legge i valori da destra a sinistra.

Questa funzione esegue operazioni regex che distinguono tra maiuscole e minuscole. In alternativa, è possibile utilizzare la variante ReplaceRegExGroupI() per eseguire operazioni regex senza distinzione tra maiuscole e minuscole.

Sintassi:

ReplaceRegExGroup (text, regex, to\_str, group [, occurrence])

Tipo di dati restituiti: stringa

Argomenti

| Argomento | Descrizione |
| --- | --- |
| text | Il testo della stringa di input che si desidera sostituire, completamente o parzialmente, con il nuovo testo di to\_str. |
| regex | L'espressione regolare che definisce quando sostituire il testo. Le corrispondenze tra questo argomento e l'argomento text vengono sostituite. |
| to\_str | Il nuovo testo che si desidera utilizzare per sostituire il contenuto esistente di text. |
| group | Il numero del gruppo, nel caso di un'espressione regolare composita.<br><br>Se l'espressione regolare contiene solo un gruppo, utilizzare invece la funzione ReplaceRegEx(). In alternativa, utilizzare ReplaceRegExGroup() con un valore group di `0`.<br><br>È possibile specificare un valore negativo per group per cercare i gruppi da destra a sinistra. |
| occurrence | Il numero della corrispondenza (tra il testo di input e l'espressione regolare) da sostituire con il nuovo testo.<br><br>Questo argomento è facoltativo. Se viene omesso, il valore predefinito è 0. Quando viene utilizzato il valore 0 o se l'argomento viene omesso, tutte le corrispondenze tra text e regex vengono sostituite con to\_str.<br><br>È possibile specificare un valore negativo per occurrence se si desidera cercare le corrispondenze da destra a sinistra. |

Esempi di funzioni

| Esempio | Risultato |
| --- | --- |
| `ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',0)` | Restituisce `abx` (sostituisce la corrispondenza completa). |
| `ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',1)` | Restituisce `abx123` (sostituisce solo il primo gruppo nella corrispondenza). |
| `ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',2)` | Restituisce `abcx` (sostituisce solo il secondo gruppo nella corrispondenza). |
| `ReplaceRegExGroup('abc123abc123','([a-z])([0-9]+)','x',1,2)` | Restituisce `abc123abx123`. Vi sono due corrispondenze tra il primo gruppo del testo di input e il modello regex. Tuttavia, solo la seconda occorrenza viene sostituita. |
| `ReplaceRegExGroup('ABC123ABC123','([a-z])([0-9]+)','x',1)` | Restituisce `ABC123ABC123`. In altre parole, non viene sostituito alcun testo. Ciò si deve al fatto che la funzione fa distinzione tra maiuscole e minuscole e la variante della funzione che non le distingue non è utilizzata. |
| `ReplaceRegExGroupI('ABC123ABC123','([a-z])([0-9]+)','x',1)` | Restituisce `ABx123ABx123`. In questo esempio, la sostituzione del testo avviene perché viene utilizzata la variante senza distinzione di maiuscole e minuscole della funzione, ReplaceRegExGroupI(). |

## Quando usare ReplaceRegExGroup

È possibile utilizzare ReplaceRegExGroup() per modificare il testo per soddisfare gli standard di formattazione e di conformità, soprattutto quando bisogna identificare modelli complessi nei dati. L'argomento group aggiuntivo per questa funzione permette di aggiornare solo alcune porzioni di un modello di testo più ampio. Ad esempio, è possibile aggiornare parti specifiche di un URL.

Se necessario, è possibile anche utilizzare questa funzione per mascherare le informazioni sensibili, come le informazioni di identificazione personale (PII), in modo che non vengano mostrate agli utenti che analizzano la propria applicazione.

## Esempio 1 - script di caricamento per aggiornare il dominio negli URL

[](https://help.qlik.com/en-US/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/ReplaceRegExGroup.htm)

#### Script di caricamento e risultati

Panoramica

Aprire l'Editor caricamento dati e aggiungere lo script di caricamento sotto in una nuova sezione.

Lo script di caricamento contiene:

* La tabella `URLs` che inizialmente contiene un elenco di stringhe di input in cui bisogna identificare e aggiornare URL specifici. Bisogna aggiornare il dominio in questi URL.

* La creazione di un campo, `ResourceURL_Updated`, che contiene un testo di input modificato con gli URL elaborati.

* Una variabile `URL_RegEx` per memorizzare l'espressione regolare utilizzata per identificare gli URL da elaborare.

Requisiti:

* Ogni stringa di input contiene due URL, con l'URL della risorsa specificato nel primo URL. Non bisogna aggiornare nessun altro URL oltre al primo dell'elenco.

* L'URL da aggiornare deve essere nel formato &lt;https o https&gt;://&lt;dominio&gt;.com/&lt;percorso risorsa&gt;. Il percorso della risorsa è opzionale, ma non viene utilizzato in questo esempio.

Script di caricamento

```
Set URL_RegEx = '(https?):\/\/(([a-zA-Z0-9]+)\.([a-zA-Z0-9]*)\.??([a-zA-Z0-9]*))\/?((([a-zA-Z0-9]*)*)((\/?([a-zA-Z0-9]*)*))*)\/{0,1}([\.,;]+(?=\s))*?';

URLs:

Load
ReplaceRegExGroup(URLsList, '$(URL_RegEx)', 'replacement-server', 2,1) as ResourceURL_Updated;

Load * Inline `
URLsList
The resource is located at https://testserver.com/files. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/worksheet. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/book. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/form. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/datamodel. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/resourcenew. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/page. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/page/overview. For more help, see: https://support.company.com.
` (delimiter is '\t');
```

Risultati

Caricare i dati e aprire un foglio. Creare una nuova tabella e aggiungere questo campo come dimensione:

* `ResourceURL_Updated`

Tabella dei risultati

| ResourceURL\_Updated |
| --- |
| The resource is located at https://replacement-server/datamodel. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files/book. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files/form. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files/worksheet. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/page. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/page/overview. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/resourcenew. For more help, see: https://support.company.com. |

## Esempio 2 - espressione del grafico per aggiornare il protocollo negli URL

[](https://help.qlik.com/en-US/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/ReplaceRegExGroup.htm)

#### Espressione del grafico e risultati

Panoramica

Aprire l'Editor caricamento dati e aggiungere lo script di caricamento sotto in una nuova sezione.

Lo script di caricamento contiene:

* La tabella `URLsList` che contiene un elenco di stringhe di input in cui bisogna identificare e aggiornare URL specifici. Bisogna aggiornare il protocollo da http a https nei collegamenti al sito web di supporto.

Requisiti:

* Ogni stringa di input contiene due URL, con il collegamento del sito web di supporto che appare come secondo URL. Non bisogna aggiornare nessun altro URL oltre al secondo dell'elenco.

* L'URL da aggiornare deve essere nel formato &lt;https o https&gt;://&lt;dominio&gt;.com/&lt;percorso risorsa&gt;. Il percorso della risorsa è opzionale, ma non viene utilizzato in questo esempio.

Script di caricamento

```
URLs:

Load * Inline `
URLsList
The resource is located at https://testserver.com/files. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/worksheet. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/book. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/form. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/datamodel. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/resourcenew. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/page. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/page/overview. For more help, see: http://support.company.com.
` (delimiter is '\t');
```

Risultati

Caricare i dati e aprire un foglio. Creare una nuova tabella e aggiungere questo campo come dimensione:

* `URLsList`

Aggiungere la seguente dimensione calcolata:

```
=ReplaceRegExGroup(URLsList, '(https?):\/\/(([a-zA-Z0-9]+)\.([a-zA-Z0-9]*)\.??([a-zA-Z0-9]*))\/?((([a-zA-Z0-9]*)*)((\/?([a-zA-Z0-9]*)*))*)\/{0,1}([\.,;]+(?=\s))*?', 'https', 1,2)
```

Tabella dei risultati

| URLsList | =ReplaceRegExGroup(URLsList, '(https?):\/\/(([a-zA-Z0-9]+)\.([a-zA-Z0-9]\*)\.??([a-zA-Z0-9]\*))\/?((([a-zA-Z0-9]\*)\*)((\/?([a-zA-Z0-9]\*)\*))\*)\/{0,1}([\.,;]+(?=\s))\*?', 'https', 1,2) |
| --- | --- |
| The resource is located at https://testserver.com/files. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/datamodel. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/files/worksheet. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/files/book. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files/book. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/files/form. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files/form. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/datamodel. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files/worksheet. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/resourcenew. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/page. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/page. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/page/overview. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/page/overview. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/resourcenew. For more help, see: https://support.company.com. |

## Ulteriori informazioni

* [Lavorare con le espressioni regolari negli script di caricamento e nelle espressioni del grafico](https://help.qlik.com/it-IT/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/regular-expressions-qlik-analytics.htm)