Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 在shell中提取get参数的值_Regex_Bash_Shell - Fatal编程技术网

Regex 在shell中提取get参数的值

Regex 在shell中提取get参数的值,regex,bash,shell,Regex,Bash,Shell,我的输入可以是dn3321或 https://domaincom/file?tag=dn3321我正在尝试使用shell解析标记的值 看起来正则表达式可以做到这一点,我如何编写一行代码来检测它是否是URL,如果它是应用正则表达式来提取值的,如果它不是直接使用值的。这个问题不清楚可能输入的全部空间是什么样子的,但是,对于您给出的简单情况,您可以使用: 这是通过删除第一个=及其前面的任何文本来实现的。如果您只需要打印出字符串的一个非常特定的部分,即url,您可以这样做: #!/bin/bash ur

我的输入可以是
dn3321
https://domaincom/file?tag=dn3321
我正在尝试使用shell解析标记的值


看起来正则表达式可以做到这一点,我如何编写一行代码来检测它是否是URL,如果它是应用正则表达式来提取值的,如果它不是直接使用值的。

这个问题不清楚可能输入的全部空间是什么样子的,但是,对于您给出的简单情况,您可以使用:


这是通过删除第一个
=
及其前面的任何文本来实现的。

如果您只需要打印出字符串的一个非常特定的部分,即url,您可以这样做:

#!/bin/bash
url="https://domaincom/file?tag=dn3321"

if [[ "$url" =~ "${http,,}" ]] ; then
        tag=$(echo $url | cut -d'=' -f2)
fi

如果您需要更详细的说明,我可以发布一个示例。

您忘记检查http;-)从OP中我不清楚可能的前缀是什么(我们应该如何处理
ftp://domaincom/file?tag=dn3321
https://domaincom/file?tag=dn3321&foo=bar
https://otherdomain.com/foo?bar=bat&tag=dn3321
?)。所以我选择了最短的例子来演示这个方法。谢谢!我现在可以调整正则表达式,但是命令的格式是我的难题,我感谢你的帮助
#!/bin/bash
url="https://domaincom/file?tag=dn3321"

if [[ "$url" =~ "${http,,}" ]] ; then
        tag=$(echo $url | cut -d'=' -f2)
fi