Reputation: 1108
I need a method to search for a substring only if it is a whole word.
Ideally I want a function (say findwstring()
) such that the two examples,
$(findwstring a, a b c)
$(findwstring a, angle b c)
should produce the values ‘a’ and ‘’ (the empty string), respectively.
I couldn't find anything here which could help me.
Upvotes: 2
Views: 2464
Reputation: 70333
$(filter a, a b c)
Produces 'a'
.
$(filter a, angle b c)
Produces ''
.
Percent (%
) in the search string is considered the wildcard character.
From the docs you linked (actually the function right below findstring
), boldface mine:
$(filter pattern…,text)
Returns all whitespace-separated words in text that do match any of the pattern words, removing any words that do not match. The patterns are written using ‘%’, just like the patterns used in the
patsubst
function above.The
filter
function can be used to separate out different types of strings (such as file names) in a variable. For example:sources := foo.c bar.c baz.s ugh.h foo: $(sources) cc $(filter %.c %.s,$(sources)) -o foo
says that
foo
depends offoo.c
,bar.c
,baz.s
andugh.h
but onlyfoo.c
,bar.c
andbaz.s
should be specified in the command to the compiler.
Upvotes: 5