Using match to find substrings in strings with only bash

后端 未结 4 2093
无人共我
无人共我 2020-12-14 00:42

Although I am almost sure this has been covered, I can\'t seem to find anything specific to this. As I continue my journey on learning bash I keep finding parts where I am b

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 01:07

    Both expressions are equivalent, the difference is the regular expression you use:

    $ echo `expr "$Stext" : '^\(.[a-z]*\)'`
    Hallo
    $ echo `expr "$Stext" : '^.[a-z]*'`
    5
    $ echo `expr "$Stext" : '\(.*World\)'`
    Hallo World
    $ echo `expr "$Stext" : '.*World'`
    11
    

    As you can see, parentheses is what makes the difference to either return the length of the match or the match itself.

    You can find more examples in Chapter 10 of the Advanced Bash-Scripting Guide.

提交回复
热议问题