正则表达式在bash中冒号后提取字符串 [英] regular expression extract string after a colon in bash

查看:113
本文介绍了正则表达式在bash中冒号后提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,我需要在:之后提取字符串:

I need to extract the string after the : in an example below:

package:project.abc.def

在那里我将得到 project.abc.def 的结果.

Where i would get project.abc.def as a result.

我正在bash中尝试此操作,我相信我有一个可以正常工作的正则表达式:([[^:] *)$ .

I am attempting this in bash and i believe i have a regular expression that will work :([^:]*)$.

在我的bash脚本中,我有 package:project.abc.def 作为名为 apk 的变量.现在我该如何在正则表达式中找到的子字符串中分配相同的变量?

In my bash script i have package:project.abc.def as a variable called apk. Now how do i assign the same variable the substring found with the regular expression?

package:project.abc.def 的结果将在 apk 变量中.而 package:project.abc.def 最初是在 apk 变量中吗?

Where the result from package:project.abc.def would be in the apk variable. And package:project.abc.def is initially in the apk variable?

谢谢!

推荐答案

这里不需要正则表达式,只需一个简单的前缀替换即可:

There is no need for a regex here, just a simple prefix substitution:

$ apk="package:project.abc.def"
$ apk=${apk##package:}
project.abc.def

##语法是bash的参数扩展之一.可以使用%代替#来修饰结尾.请参见bash手册页的本节以了解详细信息.

The ## syntax is one of bash's parameters expansions. Instead of #, % can be used to trim the end. See this section of the bash man page for the details.

一些替代方法:

$ apk=$(echo $apk | awk -F'package:' '{print $2}')
$ apk=$(echo $apk | sed 's/^package://')
$ apk=$(echo $apk | cut -d':' -f2)

这篇关于正则表达式在bash中冒号后提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆