从字符串中提取模式 [英] Extract pattern from a string

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

问题描述

在bash脚本中,从字符串中提取文本模式的简单方法是什么?

In bash script, what is the easy way to extract a text pattern from a string?

例如,我要提取X,然后在字符串末尾提取2位数字吗?

For example, I want to extract X followed by 2 digits in the end of the string?

推荐答案

使用

There's a nifty =~ regex operator when you use double square brackets. Captured groups are made available in the $BASH_REMATCH array.

if [[ $STRING =~ (X[0-9]{2})$ ]]; then
    echo "matched part is ${BASH_REMATCH[1]}"
fi

这篇关于从字符串中提取模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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