Come ottenere tutte le cifre da una stringa di frase come "Lorem 123 ipsum 456 879" => "123456879" usando regexp in ruby?Come ottenere tutte le cifre dalla stringa regexp ruby
19
A
risposta
43
Basta sostituire tutto il resto.
result = subject.gsub(/[^\d]/, '')
4
"Lorem 123 ipsum 456 879".scan(/\d+/).join # => "123456879"
6
Rif this
result = subject.gsub(/\D/, '')
1
Alcune risposte corrette già dato, che probabilmente comprende ciò che si desidera utilizzare. Un modo un po 'più a basso livello:
"Lorem 123 ipsum 456 879".chars.select {|c| c =~ /\d/}.join
7
Senza regex:
"Lorem 123 ipsum 456 879".delete('^0-9') #=>"123456879"
0
Ho sempre pensato che la cosa utilizzando staffe era più facile da leggere
"nwa240".chars.select {|s| s =~ /[0-9]/}
4
La versione da @steenslag è il il più veloce, grazie!
require 'benchmark'
class ExtractInt
def self.v1(string)
string.gsub(/[^\d]/, '')
end
def self.v2(string)
string.gsub(/\D/, '')
end
def self.v3(string)
string.delete('^0-9')
end
def self.run(m, arg)
10000.times do
self.send(m, arg)
end
end
end
Benchmark.bmbm do |x|
x.report('v1') { ExtractInt.run(:v1, 'AAAA4000') }
x.report('v2') { ExtractInt.run(:v2, 'AAAA4000') }
x.report('v3') { ExtractInt.run(:v3, 'AAAA4000') }
end
# Rehearsal --------------------------------------
# v1 0.040000 0.000000 0.040000 ( 0.041564)
# v2 0.040000 0.000000 0.040000 ( 0.042386)
# v3 0.010000 0.000000 0.010000 ( 0.011604)
# ----------------------------- total: 0.090000sec
# user system total real
# v1 0.040000 0.000000 0.040000 ( 0.042176)
# v2 0.040000 0.000000 0.040000 ( 0.043753)
# v3 0.010000 0.000000 0.010000 ( 0.012960)
Problemi correlati
- 1. Ottenere solo cifre utilizzando regexp
- 2. Come ottenere tutte le partite dalla regex?
- 3. Rimuovi tutte le occorrenze di \ dalla stringa
- 4. Ruby rimuove le righe vuote dalla stringa
- 5. Ottenere tutte le immagini dalla cartella locale
- 6. Come rimuovere tutte le emoji dalla stringa - php
- 7. Flushing tutte le cifre attuali matplotlib
- 8. python regex: ottenere le cifre finali da una stringa
- 9. Ruby global match regexp?
- 10. Genera array di tutte le lettere e cifre
- 11. Ruby - come selezionare alcuni caratteri dalla stringa
- 12. Come ottenere tutte le femmine?
- 13. Rimuovere tutte le occorrenze di carattere dalla stringa
- 14. Qual è il modo migliore per dividere una stringa per ottenere tutte le sottostringhe da Ruby?
- 15. Verificare se la stringa contiene solo le cifre
- 16. Python ottenere tutte le permutazioni dei numeri
- 17. Sostituisci tutte le non cifre con un carattere vuoto in una stringa
- 18. Ricevi tutte le immagini url da stringa
- 19. Come tagliare le parole specificate dalla stringa
- 20. Gestione ruby regexp di nbsp
- 21. trova tutte le cifre tra un personaggio in python
- 22. Verificare solo le cifre nella stringa VB.NET
- 23. Ruby Regexp: + vs *. comportamento speciale?
- 24. Ottenere numeri specifici dalla stringa
- 25. javascript stringa partita dopo regexp
- 26. come ottenere un elenco di tutte le costanti che è possibile ottenere dalla console di rotaie
- 27. Trova tutte le sottostringhe tra due stringhe
- 28. come ottenere tutte le immagini e le foto dal mio dispositivo Android non dalla sdcard?
- 29. Java: come ottenere ricorsivamente tutte le sottodirectory?
- 30. Prolog: come ottenere tutte le combinazioni
Si potrebbe anche usare \ D per [^ \ d] ...> "Lorem ipsum 123 456 879" .gsub (/ \ D /, '') => "123.456.879" – oylenshpeegul