提取键值的Ruby正则表达式
我有下面这样的绳子提取键值的Ruby正则表达式,ruby,regex,Ruby,Regex,我有下面这样的绳子 case1: str = "type=\"text/xsl\" href=\"http://skdjf.sdjhshf/CDA0000=.xsl\"" case2: str = "href=\"http://skdjf.sdjhshf/CDA0000=.xsl\" type=\"text/xsl\"" 我需要提取如下值 type -> text/xsl href -> http://skdjf.sdjhshf/CDA0000=.xsl 这是我的正则表达式,
case1:
str = "type=\"text/xsl\" href=\"http://skdjf.sdjhshf/CDA0000=.xsl\""
case2:
str = "href=\"http://skdjf.sdjhshf/CDA0000=.xsl\" type=\"text/xsl\""
我需要提取如下值
type -> text/xsl
href -> http://skdjf.sdjhshf/CDA0000=.xsl
这是我的正则表达式,它失败了
str.match(/type="(.*)"/)[1]
#this works in second case
=>"text/xsl"
str.match(/http="(.*)"/)[1]
#this works in first case
=>"http://skdjf.sdjhshf/CDA0000=.xsl"
在失败的情况下,将匹配整个字符串
有什么想法吗?同意约翰·瓦茨的评论。使用类似nokogiri的东西来解析XML——这是轻而易举的事。如果您仍然想坚持使用正则表达式解析,可以执行以下操作:
str.split(' ').map{ |part| part.match( /(.+)="(.+)"/ )[1..2] }
您将得到如下结果:
> str = "type=\"text/xsl\" href=\"http://skdjf.sdjhshf/CDA0000=.xsl\""
=> "type=\"text/xsl\" href=\"http://skdjf.sdjhshf/CDA0000=.xsl\""
> str2 = "href=\"http://skdjf.sdjhshf/CDA0000=.xsl\" type=\"text/xsl\""
=> "href=\"http://skdjf.sdjhshf/CDA0000=.xsl\" type=\"text/xsl\""
> str.split(' ').map{ |part| part.match( /(.+)="(.+)"/ )[1..2] }
=> [["type", "text/xsl"], ["href", "http://skdjf.sdjhshf/CDA0000=.xsl"]]
> str2.split(' ').map{ |part| part.match( /(.+)="(.+)"/ )[1..2] }
=> [["href", "http://skdjf.sdjhshf/CDA0000=.xsl"], ["type", "text/xsl"]]
你可以把它放在一个杂烩里或任何你想要的地方
使用nokogiri,您可以获得一个节点,然后在您的情况下执行类似于
node['href']
的操作。可能要容易得多。看起来您正在解析XML。一般来说,使用为此目的而设计的库是一个好主意。有什么特别的原因你不能或不愿意那样做吗?是的。我用的是Nokogiri。但Nokogiri只为样式表节点提供字符串。所以只有我在寻找正则表达式。Nokogiri做所有的事情,而不仅仅是css。@oldergod你能看看这个问题吗。这样你就可以理解这个问题了。这个问题是我自己解决的。谢谢你的快速回复。