可以說我有代碼:變量裏面正則表達式
str = "foobar"
print "Enter in the letters you would like to match: "
match = gets
# Pseudocode:
str =~ /[match]/
我不想整個字符串匹配:比賽,我只是想匹配的每個字母,如:
str =~ /[aeiou]/
會產生元音。 我該怎麼做才能匹配用戶輸入的字母?
可以說我有代碼:變量裏面正則表達式
str = "foobar"
print "Enter in the letters you would like to match: "
match = gets
# Pseudocode:
str =~ /[match]/
我不想整個字符串匹配:比賽,我只是想匹配的每個字母,如:
str =~ /[aeiou]/
會產生元音。 我該怎麼做才能匹配用戶輸入的字母?
試試這個:
match = gets.chomp # cut off that trailing \n
str =~ /[#{match}]/
@MichaelKohl:不,不是。至少,這就是我對問題的理解。如果用戶輸入'aeiou',則正則表達式變成'/ [aeiou] /',它匹配任何這些元音。 – 2012-04-08 18:07:26
對不起,誤讀,你當然是對的:-) – 2012-04-08 18:09:08
發生這種情況:) – 2012-04-08 18:10:38
什麼是你想在這裏實現?你在配什麼?你想查看用戶輸入的字符是否可以在'str'中找到?或者你想看看用戶是否正確寫下了'str',或者你想看看用戶是否輸入了在'str'中找到的字符? – 2012-04-08 17:59:50