在bash特定子字符串后提取令牌

编程入门 行业动态 更新时间:2024-10-27 17:12:48
本文介绍了在bash特定子字符串后提取令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有一个bash脚本包含几行的字符串变量:

Suppose I have a string variable in a bash script that contained several lines:

blah blah blah ... ... an interesting parameter: 12345 some other useless stuff... ... ...

我想从这个字符串中提取12345。我试图寻求新的方式使用'一个有趣的参数:'作为一个分隔符,但我不能完全得到它的工作。是否有这样做的一个干净的方式?

I want to extract 12345 from this string. I have tried to look for ways to use 'an interesting parameter:' as a "delimeter" but I couldn't quite get it to work. Is there a clean way of doing this?

推荐答案

庆典支持正前pression匹配,而无需使用外部程序。

bash supports regular expression matching without using external programs.

$ str=' blah blah blah ... ... an interesting parameter: 12345 some other useless stuff... ... ...' $ [[ $str =~ an\ interesting\ parameter:\ ([[:digit:]]+) ]] $ echo ${BASH_REMATCH[1]} 12345

阵列 BASH_REMATCH 包含元素0和捕获的分组全场比赛(左到右的顺序)在随后的元素。

The array BASH_REMATCH contains the full match in element 0 and the captured subgroups (in left-to-right order) in subsequent elements.

更多推荐

在bash特定子字符串后提取令牌

本文发布于:2023-10-29 22:46:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1540987.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定子   令牌   字符串   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!