用于提取键值的 Ruby 正则表达式

标签 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

这是我失败的正则表达式。
 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/

相关文章:

regex - Perl 正则表达式中的括号有什么作用?

java - Java 中的正则表达式 - 提取某些符号之间的字符串

Java - 作为 [a-zA-z0-9]* 传递的未知字符?

ruby-on-rails - 更新 gemspec 文件列表的最佳实践是什么?

mysql - 无法在 Ruby 上安装 MySQL2 0.3.16 gem

即使安装了 gem,Ruby 2.0.0 也无法加载此类文件

javascript - 使用 reduce 检查数组是否包含与字符串匹配的正则表达式

javascript - 将元素的类替换为另一个类

ruby-on-rails - rails : How does "new" action called "create" action?

ruby-on-rails - rails 3 管理员编辑另一个用户