简体   繁体   中英

get a String from a match on ruby

I have this string:

"A man, a plan, a canal -- Panama"

A regular expression:

/[^[^\w*]]/

The required result is:

"AmanaplanacanalPanama"

The problem: I cant get a string of that result. This is my code:

def palindromo(s)  #s is a string

  num_expr = /[^[^\w*]]/
  m = s.match(num_expr)
  unless m
    puts "no hubo concordancia"
    exit
  end
  print "El string de la busqueda es: " 
  puts m.string   # string donde se efectúa la búsqueda 
  print "La parte del string que concuerda con la busqueda es: " 

  puts m

The code gives me this result: "A" , and i want get this string: "AmanaplanacanalPanama"

"A man, a plan, a canal -- Panama".gsub( /[^\w]/, "" ) == "AmanaplanacanalPanama"
# => true
puts "El string de la busqueda es: #{s}"
puts s[/\W/] ? "La parte del string que concuerda con la busqueda es: #{s.gsub( /[^\w]/, "" ) }" : "no hubo concordancia"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM