Divisione di una stringa per estrarre numeriCIAO ,
Ho una stringa che è un carattere e voglio estrarre una sezione di 4 numeri. La posizione dei numeri potrebbe cambiare
Un esempio della stringa potrebbe essere
valore="Ciao 0123 esempio";
L'ho provato ma funziona solo se i numeri sono nella posizione corretta.
valore="Ciao 0123 esempio";
estratto=substr(valore,7, 4);
Poiché la posizione del numero cambierà, come posso estrarlo?
Modellazione statistica avanzataRe: Dividere una stringa per estrarre numeriok grazie @Aurora_TiffanyD E @jthi !
Re: Dividere una stringa per estrarre numeriQuesto è probabilmente meglio perché non devi prendere la prima partita.Grazie!
Re: Dividere una stringa per estrarre numeriUn'altra opzione è usare Regex invece di Regex Match (ci sono anche più opzioni su come farlo, ma penso che Regex e Regex Match siano le più facili)
Names Default To Here(1);
string = "Hello 0123 example";
four_num = Regex(string, "\d{4}"); //0123
Vedere https://regex101.com/r/26hBdb/1 per come funziona regex se non lo conosci.
Re: Dividere una stringa per estrarre numeriQuesto ha funzionato come un fascino. Grazie mille!
Re: Dividere una stringa per estrarre numeriPotresti provare qualcosa del genere:
Names Default To Here( 1 );
string = "Hello 0123 example";
matches = Regex Match(
string,
"[0-9]{4}",
);
// ASSUMES that you are interested only in the first 4-digit number, if there is more than one
extractedNumber = matches[1];
Questo restituisce "0123".
Si noti che restituisce ancora "0123" se la stringa di input è "Hello 01234 example".