gpt4 book ai didi

regex - 使用正则表达式和 bash 在 xidel 中为 xpath 表达式创建别名

转载 作者:行者123 更新时间:2023-11-29 09:27:02 24 4
gpt4 key购买 nike

如果您已经使用过 Xidel,您将经常需要定位具有特定类别的节点。为了更简单地做到这一点,我想创建 has-class("class") 函数作为表达式的别名:
contains(concat("", normalize-space(@class), ""), "class ")

示例:

$ e-xidel.sh example.com '//article/p//img[has-class("wp-image")]'

e-xidel.sh 包含这段代码:

#!/bin/bash

echo -e "$(tput setaf 2) Checking... $(tput sgr0)"

path=$1
expression=$2

# expression = '//article/p//img[has-class("wp-image")]'
# Regex to replace every * has-class("class") * by * contains(concat(" ", normalize-space(@class), " "), " class ") *
# ...
# ...
# expression = '//article/p//img[contains(concat(" ", normalize-space(@class), " "), " wp-image ")]'

xoutput=$(xidel $path --printed-node-format=html --output-declaration= -e "$expression")

echo -e "$(tput setaf 1) $xoutput $(tput sgr0)"

最佳答案

您可以使用 sed(GNU 版本,不能保证它能与其他实现一起工作)来实现您的需求:

sed 's/has-class("\([^)]\+\)")/contains(concat(" ", normalize-space(@class), " "), " \1 ")/g'

解释:

  • s/pattern/substitution/g:用substitution字符串替换与pattern匹配的部分; g 标志用于替换行的所有部分(全局替换)
  • has-class("\([^)]\+\)"):以has-class(" 包含除右括号 ([^)]) 并以 ") 结尾的任何字符。围绕内部部分的转义括号捕获子部分并将其与别名 \1 相关联,因为它是第一个创建的捕获组。
  • contains(concat("", normalize-space(@class), ""), "\1 "):用这段文字替换mached部分; \1 将被相关捕获组的内容扩展。

您的脚本将是:

#!/bin/bash

function expand-has-class() {
echo "$1" |
sed 's/has-class("\([^)]\+\)")/contains(concat(" ", normalize-space(@class), " "), " \1 ")/g'
}

echo -e "$(tput setaf 2) Checking... $(tput sgr0)"

path=$1
expression="$(expand-has-class "$2")"

# expression = '//article/p//img[has-class("wp-image")]'
# Regex to replace every * has-class("class") * by * contains(concat(" ", normalize-space(@class), " "), " class ") *
# ...
# ...
# expression = '//article/p//img[contains(concat(" ", normalize-space(@class), " "), " wp-image ")]'

xoutput=$(xidel $path --printed-node-format=html --output-declaration= -e "$expression")

echo -e "$(tput setaf 1) $xoutput $(tput sgr0)"

关于regex - 使用正则表达式和 bash 在 xidel 中为 xpath 表达式创建别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54718558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com