Ho stringhe di questo tipo:
string articoli = "01||calze bianche||3||50:::";
Che taglio tremite questo pattern ma i risultati sono "vuoti" potreste controllarmelo?
codice:string Pattern = @"^\s*||\s*(.*)\s*||\s*(.*)\s*||\s*(.*)\s*||\s*(.*)\s*$";
Ho stringhe di questo tipo:
string articoli = "01||calze bianche||3||50:::";
Che taglio tremite questo pattern ma i risultati sono "vuoti" potreste controllarmelo?
codice:string Pattern = @"^\s*||\s*(.*)\s*||\s*(.*)\s*||\s*(.*)\s*||\s*(.*)\s*$";
infatti è completamente sbagliata.
Prova con questo:
Regex regexObj = new Regex(@"[\w\s]*");
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success) {
// matched text: matchResults.Value
// match start: matchResults.Index
// match length: matchResults.Length
matchResults = matchResults.NextMatch();
}
I got the remedy
mmm non ho capito :master:
Ti faccio vedere come la usavo forse ci capiamo meglio.
In pratica devo dividere i valori della stringa in 4 diversi valori.
codice:string Pattern = @"^\s*||\s*(.*)\s*||\s*(.*)\s*||\s*(.*)\s*||\s*(.*)\s*$"; Regex regexObj = new Regex(@"[\w\s]*"); for (int i = 0; i < arStr.Length; i++) { Regex re = new Regex(Pattern); string source = arStr[i]; Response.Write(arStr[i] + " "); if (!re.IsMatch(source)) continue; MatchCollection mc = re.Matches(source); //separo i campi string Gruppo1 = mc[0].Groups[1].Value; string Gruppo2 = mc[0].Groups[2].Value; string Gruppo3 = mc[0].Groups[3].Value; string Gruppo4 = mc[0].Groups[4].Value; Response.Write(Gruppo1 + " "); Response.Write(Gruppo2 + " "); Response.Write(Gruppo3 + " "); Response.Write(Gruppo4 + " "); }
se vuoi separare
"01||calze bianche||3||50:::"; in
01
calze bianche
3
50
l'espressione che ho scritto io va bene..
I got the remedy
eh ecco ho scordato un pezzo mea culpa :master:
Conta che nel ciclo la stringa che mi arriva è
"01||calze bianche||3||50";
ho provato la tua espressione e mi continua a dare campi vuoti, mi sa che la causa siano i :::
prova il codice che ho scritto io e ti posso assicurare che funziona
I got the remedy
Ho fatto così:
Il risultato è:codice:string a= "01||calze bianche||3||50:::"; a= a+ "02||calze verdi||5||20:::"; string a = articoli; Regex regexObj = new Regex(@"[\w\s]*"); Match matchResults = regexObj.Match(a); while (matchResults.Success) { Response.Write("value" + matchResults.Value + " "); Response.Write("index" + matchResults.Index + " "); Response.Write("lenght" + matchResults.Length + " "); matchResults = matchResults.NextMatch(); }
value: 01
index: 0
lenght: 2
value:
index: 2
lenght: 0
value:
index: 3
lenght: 0
value: calze bianche
index: 4
lenght: 13
value:
index: 17
lenght: 0
value:
index: 18
lenght: 0
value: 3
index: 19
lenght: 1
value:
index: 20
lenght: 0
value:
index: 21
lenght: 0
value: 50
index: 22
lenght: 2
value:
index: 24
lenght: 0
value:
index: 25
lenght: 0
value:
index: 26
lenght: 0
value: 02
index: 27
lenght: 2
value:
index: 29
lenght: 0
value:
index: 30
lenght: 0
value: calze verdi
index: 31
lenght: 11
value:
index: 42
lenght: 0
value:
index: 43
lenght: 0
value: 5
index: 44
lenght: 1
value:
index: 45
lenght: 0
value:
index: 46
lenght: 0
value: 20
index: 47
lenght: 2
value:
index: 49
lenght: 0
value:
index: 50
lenght: 0
value:
index: 51
lenght: 0
value:
index: 52
lenght: 0
---
Il concetto c'è ma non capisco il mio codice sopra dove è sbagliato. Perchè mi servirebbe avere ad ogni ciclo 4 stringhe con i 4 valori.
avevi ragione, ho sbagliato io
questa è quella corretta
Regex regexObj = new Regex(@"[\w\s]*[\w\s-[\t\r\n]]");
l'ho testato e funziona
I got the remedy
Grazie mille!
Mi sto scervellando per adattarlo al mio codice e non ci salto fuori.
Il mio problema sostanzialmente è che per ogni riga ho bisogno dei 4 valori divisi e così o i valori singoli uno a uno.
Non so se mi sono spiegata.
Praticamente su ogni riga devo riuscire a fare 4 variabili, quando queste sono complete metterle in un datarow e passare alla riga successiva.
non ho capito però intuisco che sostanzialmente devi fare dei cicli annidati.
Se non è lungo, posta il tuo codice e prova a spiegarmi meglio cosa ti serve
![]()
I got the remedy