我有一个字符串数组,如下所示
["<table class=\"size-table _size-table\">\n<tr class=\"product-size _product-size disabled _disabled\" data-sku=\"3255138\" role=\"option\" aria-disabled=\"true\" aria-label=\"2\">\n<td class=\"size-name _size-name\">2",
"\n<td class=\"subscribe\">",
"\n</tr>\n<tr class=\"product-size _product-size disabled _disabled\" data-sku=\"3255136\" role=\"option\" aria-disabled=\"true\" aria-label=\"3\">\n<td class=\"size-name _size-name\">3",
"\n<td class=\"subscribe\">",
"\n</tr>\n<tr class=\"product-size _product-size disabled _disabled\" data-sku=\"3255137\" role=\"option\" aria-disabled=\"true\" aria-label=\"4\">\n<td class=\"size-name _size-name\">4",
"\n<td class=\"subscribe\">",
"\n</tr>\n<tr class=\"product-size _product-size disabled _disabled\" data-sku=\"3255135\" role=\"option\" aria-disabled=\"true\" aria-label=\"5\">\n<td class=\"size-name _size-name\">5",
"\n<td class=\"subscribe\">",
"\n</tr>\n<tr class=\"product-size _product-size disabled _disabled\" data-sku=\"3255134\" role=\"option\" aria-disabled=\"true\" aria-label=\"6\">\n<td class=\"size-name _size-name\">6",
"\n<td class=\"subscribe\">",
"\n</tr>\n<tr class=\"product-size _product-size disabled _disabled\" data-sku=\"3255133\" role=\"option\" aria-disabled=\"true\" aria-label=\"7\">\n<td class=\"size-name _size-name\">7",
"\n<td class=\"subscribe\">",
"\n</tr>\n<tr class=\"product-size _product-size disabled _disabled\" data-sku=\"3255132\" role=\"option\" aria-disabled=\"true\" aria-label=\"8\">\n<td class=\"size-name _size-name\">8",
"\n<td class=\"subscribe\">",
"\n</tr>\n<tr class=\"product-size _product-size disabled _disabled\" data-sku=\"3255131\" role=\"option\" aria-disabled=\"true\" aria-label=\"9\">\n<td class=\"size-name _size-name\">9",
"\n<td class=\"subscribe\">",
"\n</tr>\n</table>\n"]
我想要aria-label中的内容。这将是上面显示的数字,或者在某些情况下,它可能是一些值,如S,M,L,XL。
所以我正在尝试每个数组元素,并通过ruby中的某种正则表达式选择aria-label下的内容。但我无法正确理解它。请帮忙
答案 0 :(得分:2)
虽然有人认为不应该用正则表达式解析HTML,但在这种特殊情况下,它可能被认为是好的恕我直言,因为输入更像是字符串,而不像HTML。
inp.map { |e| e[/(?<=aria-label=").+?(?=")/] }
#⇒ ["2", nil, "3", nil, "4", nil, "5", nil, "6",
# nil, "7", nil, "8", nil, "9", nil, nil]
仅检索有意义的值:
inp.map { |e| e[/(?<=aria-label=").+?(?=")/] }.compact
#⇒ ["2", "3", "4", "5", "6", "7", "8", "9"]