Ayuda de la expresión regular
-
14-11-2019 - |
Pregunta
Actualmente estoy haciendo un montón de procesamiento en una cadena usando expresiones regulares con gsub()
, pero los estoy encadenando bastante, lo que está empezando a estar desordenado.¿Puedes ayudarme a construir una sola regulación para lo siguiente:
string.gsub(/\.com/,'').gsub(/\./,'').gsub(/&/,'and').gsub(' ','-').gsub("'",'').gsub(",",'').gsub(":",'').gsub("#39;",'').gsub("*",'').gsub("amp;",'')
básicamente lo anterior elimina lo siguiente:
- .com
- .
- ,
- :
- *
- switches '&' for 'y'
- switches '' for '-'
- cambia 'para' '
¿Hay una manera más fácil de hacer esto?
Solución
You can combine the ones that remove characters:
string.gsub(/\.com|[.,:*]/,'')
The pipe |
means "or". The right side of the or is a character class; it means "one of these characters".
Otros consejos
A translation table is more scalable as you add more options:
translations = Hash.new
translations['.com'] = ''
translations['&'] = 'and'
...
translations.each{ |from, to| string.gsub from, to }
Building on Tim's answer:
You can pass a block to String.gsub
, so you could combine them all, if you wanted:
string.gsub(/\.com|[.,:*& ']/) do |sub|
case(sub)
when '&'
'and'
when ' '
'-'
else
''
end
end
Or, building off echoback's answer, you could use a translation hash in the block (you may need to call translations.default = ''
to get this working):
string.gsub(/\.com|[.,:*& ']/) {|sub| translations[sub]}
The biggest perk of using a block is only having one call to gsub
(not the fastest function ever).
Hope this helps!