work.bin
work.bin

Reputation: 1108

How to search for whole words in a Makefile string?

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

Answers (1)

DevSolar
DevSolar

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 of foo.c, bar.c, baz.s and ugh.h but only foo.c, bar.c and baz.s should be specified in the command to the compiler.

Upvotes: 5

Related Questions