So this is what I have currently in my shell script. I am pretty bad at regular expressions.
所以这就是我目前在shell脚本中的内容。我在正则表达式上非常糟糕。
echo $1 | grep -E '^[A-Z]${3}|^[0-9]${4}|^[ABCD]$'
I need my input like this:
我需要这样的输入:
./dlchek.sh ACV-1319-A
This is the general format: XYX-1111-A where XYX can be any letter, 1111 can be any number, and A has to be either (A,B,C,D). I am using unix by the way. I only need to check if the format is correct and exit with code 0 if it is correct or 1 if it is incorrect.
这是一般格式:XYX-1111-A,其中XYX可以是任何字母,1111可以是任何数字,A必须是(A,B,C,D)。我顺便使用unix。我只需要检查格式是否正确,如果正确则退出代码0,如果不正确则退出1。
This is expected output: Where line 3 is the only one that worked because it followed format.
这是预期的输出:第3行是唯一有效的,因为它遵循格式。
[you@blue final]$ ./dlcheck.sh ACV-13-A
[you@blue final]$ echo $?
1
[you@blue final]$ ./dlcheck.sh ACV-13119-A
[you@blue final]$ echo $?
1
[you@blue final]$ ./dlcheck.sh ACV-1319-A
[you@blue final]$ echo $?
0
[you@blue final]$ ./dlcheck.sh ACV-1319-Y
[you@blue final]$ echo $?
1
1 个解决方案
#1
0
^[A-Z]{3}-[0-9]{4}-[ABCD]$
What are those $$ doing in there? too many endings. And those pipes?? Do like me, use https://regex101.com/ and you'll learn something every time..
那些$$在那里做什么?太多的结局。那些管子?跟我一样,使用https://regex101.com/,你每次都会学到一些东西......
#1
0
^[A-Z]{3}-[0-9]{4}-[ABCD]$
What are those $$ doing in there? too many endings. And those pipes?? Do like me, use https://regex101.com/ and you'll learn something every time..
那些$$在那里做什么?太多的结局。那些管子?跟我一样,使用https://regex101.com/,你每次都会学到一些东西......