击弦提取/操纵 [英] Bash string extraction / manipulation

查看:167
本文介绍了击弦提取/操纵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种复杂的字符串,格式为

I have a kind of complicated string, the form is

XXXP + NUMyyy

"xxxp+NUMyyy"

其中xxx,NUM和YYY都变长,而'+'​​可以是一个数学运算符,如' - ','*','/',或'='。

where xxx, NUM, and yyy are all variable length, and '+' can be a mathematical operator, such as '-', '*', '/', or '='.

我试图找出最佳的方式来获得用户输入什么数学运算符和数量。

I am trying to figure out the best way to get what mathematical operator and number the user has entered.

我试着用的东西组合是这样的:

I tried using combination of things like this:

    echo `expr match "tcp+111" '\([+-=*/]\)'`
    echo `expr match "tcp+111" '\(\+\-=\*/\)'`

没什么迄今奏效。我在考虑做这样的事情是使用正前pressions最简单的方法,但也许我错了?什么是做到这一点的好办法?

Nothing has worked thus far. I'm thinking the easiest way to do such a thing is by using regular expressions, but maybe I'm wrong? What is a good way to do this?

感谢你。

例如输入tcjp-100,P + 1,P + 1:调试,CP = 11:V。我没有忘记提及,经营者总会有字母P之前。此外,XXX和YYY不必是present,但也可以是

example input: "tcjp-100" "p+1" "p+1:debug" "cp=11:v". I did forget to mention, before the operator there will always be the letter 'p'. In addition, 'xxx' and 'yyy' do not have to be present, but can be

推荐答案

您可以使用猛砸的正则表达式匹配功能。

You can use Bash's regex matching feature.

string="xxxp+3456yyy"
pattern="[^*/+-]*([*/+-]*)([[:digit:]]+).*"    # the hyphen must come last (or first, but after ^) in the character sets

[[ $string =~ $pattern ]]
echo "${BASH_REMATCH[1]}"    # operator
echo "${BASH_REMATCH[2]}"    # number

这篇关于击弦提取/操纵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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