2010-01-02 16 views

回答

5

依赖,如果他们都像她那样,那么:

/Phone \+ (\w+) mins & (\w+) texts - (\d+) month plan \$(\w+)/

这假定计划可能包含无限分钟。

您可以使用这样的正则表达式:

str = "Phone + 300 mins & unlimited texts - 24 month plan $25" 
regex = /Phone \+ (\w+) mins & (\w+) texts - (\d+) month plan \$(\w+)/ 
match = regex.match(str).to_a 

现在的比赛是["Phone + 300 mins & unlimited texts - 24 month plan $25", "300", "unlimited", "24", "25"]

0

匹配,也可以与=~

所以简称:

string =~ /Phone\s*\+\s*(\w*)\s*mins\s*&\s*(\w*)\s*texts\s*-\s*(\w*)\s*month\s*plan\s*\$(\w*)/ 

执行匹配字符串与右边的正则表达式ha旁边。

也可以直接访问在这种情况下的一组利用$ 1值(括号内的正则表达式的部分)等

所以

minutes = $1 
texts = $2 
months = $3 
cost = $4