我有像下面这样的字符串
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"
在失败的情况下,匹配整个字符串。
有什么想法吗?
最佳答案
同意 John Watts 的评论。使用类似 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']
的操作。在你的情况下。可能要容易得多。
关于用于提取键值的 Ruby 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13066639/