come estrarre la stringa tra un modello specifico utilizzando regexp?
CIAO,
Sono nuovo in regex, devo estrarre il nome degli utenti dal loro ID, ad esempio:
Ho la seguente lunga stringa
123450_dan; 154396_kelli; 198756_dev;
Il risultato deve essere: dan
Quale regex devo usare per estrarre il nome dan che ha l'id 123450?
Grazie in anticipo.
Ri: come estrarre la stringa tra pattern specifici utilizzando regexp?Grazie, funziona perfettamente!!
Ri: come estrarre la stringa tra pattern specifici utilizzando regexp?Grazie, funziona perfettamente.
Ri: come estrarre la stringa tra pattern specifici utilizzando regexp?Utilizza invece questo modulo:
name = Regex( string, "123450_([a-zA-Z ]+);", "\1" );
Ri: come estrarre la stringa tra pattern specifici utilizzando regexp?Lo script che ho fornito in precedenza utilizzando le funzioni JMP non richiederebbe alcuna modifica.L'espressione regolare per catturare uno o più "nomi" dopo il carattere di sottolineatura sarebbe (\w+\s*)+ che significa accumulare ( ) almeno una parola \w+ , zero o più spazi \S* e il segno più finale implica ripetere lo schema una o più volte.
Names Default to Here(1);
string="123450_dan fox; 154396_kelli ann marie; 198756_dev;";
name1 = Regex( string, "123450_((\w+\s*)+\b)", "\1" );
name2 = Regex( string, "154396_((\w+\s*)+\b)", "\1" );
Show( name1, name2 ); <BR />/*: name1 = "dan fox"; name2 = "kelli ann marie"; */
Ri: come estrarre la stringa tra pattern specifici utilizzando regexp?Ciao Mark,
Solo 1 domanda, cosa devo aggiungere se ho spazio nel nome utente:
ad esempio: "123450_dan volpe; 154396_kelli; 198756_dev;"
Ho provato il seguente Regex:
string="123450_dan volpe; 154396_kelli; 198756_dev;"
nome = Regex( stringa, "123450_(\w+ | \s+)", "\1" );
Mostra(nome);
Risultato: "dan" invece di "dan fox"
Grazie in anticipo.
Ri: come estrarre la stringa tra pattern specifici utilizzando regexp?Solo per divertimento e poiché le espressioni regolari possono essere difficili da leggere per altri (o due settimane dopo), ecco un'alternativa, utilizzando le funzioni JMP.
Names Default to Here( 1 );
// test string
string = "123450_dan; 154396_kelli; 198756_dev;";
//create a list of id_name
id = Words(string, ";");
//create a list if ids, a list if names, an associative array(keyed list)
idlist={};
namelist={};
lookup = [=>""]; //Associative Array that retuns an empty string, if number is not valid
for(i=1, i<=nitems(id), i++,
InsertInto(idlist, num(word(1,Trim(id[i]),"_")) );
InsertInto(namelist, Titlecase(word(2,Trim(id[i]),"_")) ); //don't need TitleCase
lookup[idlist[i]]=namelist[i] //ids are the keys and names are the values
);
show(id, idlist, namelist, lookup, lookup[198756], lookup[128954]); //last one is not valid
Ri: come estrarre la stringa tra pattern specifici utilizzando regexp?Grazie Marco,
Funziona perfettamente!!Ri: come estrarre la stringa tra pattern specifici utilizzando regexp?Supponendo che tu voglia associare il nome a un ID specifico nella parte "id_name;", anche questa versione funzionerà.
Names Default to Here( 1 );
// test string
string = "123450_dan; 154396_kelli; 198756_dev;";
// target ID
id = "123450";
// assume specific ID
name = Regex( string, id || "_(\w+);", "\1" );
Show( name );
Ri: come estrarre la stringa tra pattern specifici utilizzando regexp?Non hai specificato molti dettagli sull'ID, quindi ecco alcuni esempi per iniziare. Ho utilizzato uno script JMP, ma questa estrazione potrebbe essere utilizzata anche in una formula di colonna.
Names Default to Here( 1 );
// test string
string = "123450_dan; 154396_kelli; 198756_dev;";
// assume specific ID
name = Regex( string, "123450_(\w+\b)", "\1" );
Show( name );
// assume first ID with 6 numbers
name = Regex( string, "\d{6}_(\w+\b)", "\1" );
Show( name );
// assume first ID with any number of numbers
name = Regex( string, "\d+_(\w+\b)", "\1" );
Show( name );