# Presentazione + Problema su match stringhe

**URL:** <https://www.ruby-forum.com/t/presentazione-problema-su-match-stringhe/129011>\
**Category:** IT\
**Created:** [February 13, 2008, 2:57pm UTC](https://www.ruby-forum.com/t/presentazione-problema-su-match-stringhe/129011 "2008-02-13T14:57:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![haltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4da419/32.png) [@haltman](https://www.ruby-forum.com/u/haltman)\
**Post date:** [February 13, 2008, 2:57pm UTC](https://www.ruby-forum.com/t/presentazione-problema-su-match-stringhe/129011/1 "2008-02-13T14:57:37Z")

</div>

Ho provato a modificare il mio codice  
così:  
print “Stringa da cercare:\n”  
stringa = gets.chomp!.to\_s  
puts “stringa-\>” + stringa + “\n”  
matching\_lines = \*open(‘prova.txt’, ‘r’).select { |l| l.match /stringa/  
}  
if matching\_lines != nil  
matching\_lines.each{|x| print x, “\n”}  
else  
puts “Nessuna corrispondenza trovata!”  
end

solo che la parte del regular expression non funziona non riesco a  
fargli “capire” il mio pattern

grazie ancora!

---

<div class="post-metadata">

**Author:** ![haltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4da419/32.png) [@haltman](https://www.ruby-forum.com/u/haltman)\
**Post date:** [February 13, 2008, 3:25pm UTC](https://www.ruby-forum.com/t/presentazione-problema-su-match-stringhe/129011/2 "2008-02-13T15:25:50Z")

</div>

> > Ho provato a modificare il mio codice così:
> > 
> > print “Stringa da cercare:\n”  
> > stringa = gets.chomp!.to\_s  
> > puts “stringa-\>” + stringa + “\n”  
> > matching\_lines = \*open(‘prova.txt’, ‘r’).select { |l| l.match /stringa/  
> > }

Prova:

matching\_lines = \*open(‘prova.txt’, ‘r’).select { |l| l.include? stringa  
}

Include torna true se la stringa contiene l’altra stringa.

---

<div class="post-metadata">

**Author:** ![haltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4da419/32.png) [@haltman](https://www.ruby-forum.com/u/haltman)\
**Post date:** [February 13, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/presentazione-problema-su-match-stringhe/129011/3 "2008-02-13T15:08:46Z")

</div>

non ho provato ma forse al posto del select potresti usare String#scan

qualcosa tipo:  
matching\_lines = contenuto\_del\_file.scan /^._stringa._$/ #=\> array  
delle righe che matchano

non so se è quello il problema, sono troppo pigro per provare 🙂  
Paolo

On Feb 13, 2008 2:50 PM, haltman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> solo che la parte del regular expression non funziona non riesco a fargli “capire” il mio pattern
> 
> grazie ancora!
> 
> * * *
> 
> Ml mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.ruby-it.org/mailman/listinfo/ml](http://lists.ruby-it.org/mailman/listinfo/ml)

–  
Paolo D.’  
SeeSaw | Intuitive web stuff

> **[A Padova il buon software chi lo fa?](https://seesaw.it/)**
>
> A creare software sono tantissimi, a farlo molto bene c’è una certa schiera di sviluppatori, ma a costruirlo un pezzo alla volta insieme al cliente, plasmarlo sulle sue esigenze e renderlo parte di un nuovo approccio al mondo del lavoro sono davvero...

removed\_email\_address@domain.invalid  
personal [http://paolodona.blogspot.com](http://paolodona.blogspot.com)

---

<div class="post-metadata">

**Author:** ![haltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4da419/32.png) [@haltman](https://www.ruby-forum.com/u/haltman)\
**Post date:** [February 13, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/presentazione-problema-su-match-stringhe/129011/4 "2008-02-13T15:28:16Z")

</div>

Io farei  
così:  
matching\_lines = File.readlines(filename) {|l| l.match(/stringa/)}

---

<div class="post-metadata">

**Author:** ![haltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4da419/32.png) [@haltman](https://www.ruby-forum.com/u/haltman)\
**Post date:** [February 13, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/presentazione-problema-su-match-stringhe/129011/5 "2008-02-13T15:28:51Z")

</div>

Oops, mancava il select:

> matching\_lines = File.readlines(filename).select {|l| l.match(/  
> stringa/)}

Il giorno 13/feb/08, alle ore 15:27, Giovanni I. ha scritto:

---

<div class="post-metadata">

**Author:** ![haltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4da419/32.png) [@haltman](https://www.ruby-forum.com/u/haltman)\
**Post date:** [February 13, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/presentazione-problema-su-match-stringhe/129011/6 "2008-02-13T15:38:09Z")

</div>

haltman wrote:

> Ho provato a modificare il mio codice  
> cosï¿½:  
> print “Stringa da cercare:\n”  
> stringa = gets.chomp!.to\_s  
> puts “stringa-\>” + stringa + “\n”  
> matching\_lines = \*open(‘prova.txt’, ‘r’).select { |l| l.match /stringa/  
> }  
> if matching\_lines != nil  
> matching\_lines.each{|x| print x, “\n”}  
> else  
> puts “Nessuna corrispondenza trovata!”  
> end
> 
> solo che la parte del regular expression non funziona non riesco a  
> fargli “capire” il mio pattern
> 
> grazie ancora!

Come suggeriva Stefano, sostituisci il

l.match /stringa/

con

l.match Regexp.new(stringa)

---

<div class="post-metadata">

**Author:** ![haltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4da419/32.png) [@haltman](https://www.ruby-forum.com/u/haltman)\
**Post date:** [February 13, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/presentazione-problema-su-match-stringhe/129011/7 "2008-02-13T15:49:01Z")

</div>

> > Ho provato a modificare il mio codice  
> > cosï¿½:  
> > print “Stringa da cercare:\n”  
> > stringa = gets.chomp!.to\_s  
> > puts “stringa-\>” + stringa + “\n”  
> > matching\_lines = \*open(‘prova.txt’, ‘r’).select { |l| l.match /stringa/  
> > }

funziona anche

matching\_lines = \*open(‘prova.txt’, ‘r’).select { |l| l.match stringa }

senza gli slash

---

<div class="post-metadata">

**Author:** ![haltman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4da419/32.png) [@haltman](https://www.ruby-forum.com/u/haltman)\
**Post date:** [February 14, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/presentazione-problema-su-match-stringhe/129011/8 "2008-02-14T16:18:42Z")

</div>

Ultimo aggiornamento:

Sono riuscito a fare funzionare il codice cosi’:

matching\_lines = \*open(‘prova.txt’, ‘r’).select { |l| l.match  
Regexp.new(stringa)}

perchÃ¨ mettendo l.match /stringa/ il codice non mi interpreta la mia  
variabile come stringa da passare nel pattern.

ciao e grazie ancora a tutti!!
