Vai al contenuto


Foto

regex experts


Questa discussione e' stata archiviata Questo significa che non e' possibile rispondere
41 risposte a questa discussione

#1 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 04 aprile 2012 - 13:55

Se chiedo al tuttologo comincia con un "c'era una volta il regex" e non ho tempo :pua:

Da una stringa così:
DLH3029 (LUFTHANSA THREE ZERO TWO NINER)

deve uscire una stringa così:
'LUFTHANSA' 'THREE' 'ZERO' 'TWO' 'NINER'

quindi togliere la prima word, togliere le parentesi e quotare ogni word in single quotes.

how to ?
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#2 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 04 aprile 2012 - 13:57

Fai un ciclo che si passi tutti gli spazi. Levi la prima parola (tutti i caratteri che precedono il primo spazio e lo spazio stesso), metti un apice.

Cancelli ogni simbolo ( e ) che trovi.

Dal secondo spazio in poi, metti un apice prima e dopo ogni carattere spazio.

Chiudi la stringa con un apice.

Profit.
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#3 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 04 aprile 2012 - 14:13

Ma va a cagher :asd:
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#4 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 04 aprile 2012 - 14:13

btw: non notifica più il forum
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#5 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 04 aprile 2012 - 14:14

Comunque, se ti può consolare, ogni volta che devo fare qualcosa di simile con UltraEdit mi viene da piangere.

Su 10 volte una ci riesco, le altre o le faccio a mano o mi faccio un programma in VB... :asd:
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#6 yorkeiser

yorkeiser

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 68.968 Messaggi:

Inviato 04 aprile 2012 - 14:29

Se il problema è sempre quello e non è più generale, imho per una cosa del genere bastano una replace, una substring e un append, scomodare le regex mi sembra come usare un bazooka per sparare a una mosca...

Il sole è giallo

Brava Giovanna, brava

E canto please don't let me be misunderstood mentre parcheggio nel parcheggio l'alfasud


#7 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 04 aprile 2012 - 14:44

Pfui, era facilissimo :challenge:

echo "DLH3029 (LUFTHANSA THREE ZERO TWO NINER)" | sed "s/[^(]*(\([^)]*\)).*/'\1'/" | sed "s/[[:space:]]\+/' '/g"

'LUFTHANSA' 'THREE' 'ZERO' 'TWO' 'NINER'


Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#8 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 04 aprile 2012 - 14:46

:dumb:

Ci avrei messo una settimana per trovare quella sequenza di caratteri...

:lol:
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#9 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 04 aprile 2012 - 14:48

Alla fine ho chiesto al tuttologo visto che l'ho aiutato coi db, cosa di cui lui non capisce un cazzo :yeah:

Il bello è che m'ha detto che non è possibile visto che non si possono fare loops col sed.

Son rimasto basito! gli ho detto "cristo Wolfgang, no lo so fare col regex ma prima elimini la prima parola e sostituisci le () coi quotes poi sostituisci gli spazi col quote+spazio+quote'".
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#10 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 04 aprile 2012 - 14:49

:dumb:

Ci avrei messo una settimana per trovare quella sequenza di caratteri...

:lol:


La prima sostituzione l'ha azzeccata alla prima botta, ci son rimasto così :blink:
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#11 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 04 aprile 2012 - 14:49

Comunque odio le expression per la sostituzione dei caratteri. Mi fanno venire l'orchite.
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#12 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 04 aprile 2012 - 14:54

Comunque odio le expression per la sostituzione dei caratteri. Mi fanno venire l'orchite.


Vero ma sono di una potenza impressionante.

A me servono perché voglio inserire nel db la regola dell'estrazione della stringa, cosa che il programma non dovrebbe conoscere.
Così è tutto dinamico (sono maniaco della dinamicità :okay: ) e per stabilire una nuova regola basta inserire nel db una nuova regex che il programma carica in real time.

Messaggio modificato da trallallero il 04 aprile 2012 - 14:55

Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#13 yorkeiser

yorkeiser

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 68.968 Messaggi:

Inviato 04 aprile 2012 - 14:54

Idem. Il tutto poi nella shell (sintassi davvero di merda imho, parlo di bash, che è poi l'unica che conosco) rende il mondo un posto più brutto

Il sole è giallo

Brava Giovanna, brava

E canto please don't let me be misunderstood mentre parcheggio nel parcheggio l'alfasud


#14 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 04 aprile 2012 - 14:56

Avevo dovuto imparare massivamente la sintassi di quella robaccia quando facevo Perl all'uni.

Ho ancora gli incubi...
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#15 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 04 aprile 2012 - 15:01

Idem. Il tutto poi nella shell (sintassi davvero di merda imho, parlo di bash, che è poi l'unica che conosco) rende il mondo un posto più brutto


Poraccio :patpat:


Ora tocca vedere se Qt accetta una regex del genere.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#16 yorkeiser

yorkeiser

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 68.968 Messaggi:

Inviato 04 aprile 2012 - 15:10

Poraccio :patpat:


Ora tocca vedere se Qt accetta una regex del genere.


Dai trolla, sono d'accordo per la potenza, la robustezza, la versatilità e quello che ti pare, ma la sintassi è davvero merdosa.
Manco il cobol è così brutto da leggere :pua:

Il sole è giallo

Brava Giovanna, brava

E canto please don't let me be misunderstood mentre parcheggio nel parcheggio l'alfasud


#17 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 04 aprile 2012 - 15:15

Da come scrivi però sembra che tu odi la bash e questo non lo perdono :challenge:

Si ma ora qualcuno sa come far eseguire 'sta regex a Qt ?
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#18 TigerShark

TigerShark

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.685 Messaggi:

Inviato 04 aprile 2012 - 16:02

mah... io avrei fatto qualcosa del genere in c# che almeno e' comprensibile da tutti a differenza di quell'immonda serie di caratteri...



class Program

{

	static void Main(string[] args)

	{

		const string original = "DLH3029 (LUFTHANSA THREE ZERO TWO NINER)";

		var splitted = original.Remove("(").Remove(")").Split();

		var result = "";

		splitted.ToList().ForEach(s => result += string.Format("'{0}' ", s));

		result = result.Remove(result.Length - 1);

		Console.WriteLine(result);

	}

}



static class StringExtensions

{

	public static string Remove(this string s, string c)    { return s.Replace(c, ""); }

}

sara' pure piu' lungo, ma almeno si capisce cosa fa....
I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past I will turn the inner eye to see its path. Where the fear has gone there will be nothing. Only I will remain.

#19 yorkeiser

yorkeiser

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 68.968 Messaggi:

Inviato 04 aprile 2012 - 16:10

Da come scrivi però sembra che tu odi la bash e questo non lo perdono :challenge:

Si ma ora qualcuno sa come far eseguire 'sta regex a Qt ?


Assolutamente; la bash è splendida a livello di funzionalità, è la sintassi che è una merda.
Mai provato a leggere script di migliaia di righe? Non so te, ma a me viene l'orticaria.
Per contro, il codice postato dallo squaloTigre lo capisce pure uno che non ha mai visto c#, e lo si legge in 10 secondi se sei bravo, 30 se sai almeno accendere un computer :asd:

Il sole è giallo

Brava Giovanna, brava

E canto please don't let me be misunderstood mentre parcheggio nel parcheggio l'alfasud


#20 TigerShark

TigerShark

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.685 Messaggi:

Inviato 04 aprile 2012 - 16:14

ovvio, l'unica parte un po' ostica per chi non conosce c# e' l'uso di linq per fare il ciclo e l'extension method che ho usato per fare un simil-DSL dato che non mi andava di usare il replace per due volte dato che appesantiva la sintassi. :sisi:
I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past I will turn the inner eye to see its path. Where the fear has gone there will be nothing. Only I will remain.