Does bash support word boundary regular expressions?

前端 未结 8 1650
面向向阳花
面向向阳花 2020-12-01 05:30

I am trying to match on the presence of a word in a list before adding that word again (to avoid duplicates). I am using bash 4.2.24 and am trying the below:



        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 05:50

    I've used the following to match word boundaries on older systems. The key is to wrap $foo with spaces since [^[:alpha:]] will not match words at the beginning or end of the list.

    [[ " $foo " =~ [^[:alpha:]]myword[^[:alpha:]] ]]
    

    Tweak the character class as needed based on the expected contents of myword, otherwise this may not be good solution.

提交回复
热议问题