I am not sure whether I have described the question properly, but currently I am solving this problem in the following way
QUOTEDSTR := "hello world"
NORMALSTR := $(shell echo $(QUOTEDSTR))
Is there a more built-in way that 'make' can do this without calling shell? Thanks
To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.
Use method String. split() It returns an array of String, splitted by the character you specified.
You can escape quotes by preceding them with a \ like so : my_var = "Tabvxc \"avcx\"sdasaf\" sadasfdf. sdsadsaf '0000000000000000000000000000000'."
Another option:
NORMALSTR := $(patsubst "%",%,$(QUOTEDSTR))
Beta's answer will remove every quote in the string. The above solution will ONLY remove quotes that appear at the beginning and end. For example:
QUOTEDSTR := -DTITLE=\"Title\"
Beta's answer will result in a value of -DTITLE=\Title\
while using the patsubst solution this value will not be changed.
It depends on what you want.
EDIT
If you want to handle whitespace and still only match quotes at the beginning/end of the variable as per @stefanct's comment, you'll have to play some tricks. First you need to find a non-whitespace character which you know will never appear in your string. Let's choose ^
but you can choose something else if you want.
The algorithm is: convert all spaces to this character, then remove the quotes from the resulting single "word", then convert all instances of that character back to spaces, like this:
# Get a variable S that contains a single space
E :=
S := $E $E
NORMALSTR := $(subst ^,$S,$(patsubst "%",%,$(subst $S,^,$(QUOTEDSTR))))
Of course there are still complications; this handles only spaces for example, not other whitespace characters like TAB.
All answers yet have issues. patsubst
"[f]inds whitespace-separated words" so the simple solution by @MadScientist does not work for strings like "hello world"
. The one by @Beta on the other hand removes all quote characters no matter where they are.
The code below shows how to deal with strings with spaces in them as well. However, it will also remove other quote characters on the edge of words, for example "hello "world"3"
will be transformed to hello world"3
. If that's any better... I don't know, probably not.
Instead of the other solutions this one creates a user function named unquote
instead of directly replacing the strings.
quoted="hello world"
unquote = $(patsubst "%,%,$(patsubst %",%,$(1)))
#unquote = $(subst $\",,$(1))
#unquote = $(patsubst "%",%,$(1))
#unquote = $(shell echo $(1))
unquoted = $(call unquote,$(quoted))
$(info Variable quoted is $(quoted))
$(info Variable unquoted is $(unquoted))
This simply looks for all quote characters at the start and end of every (white-space separated) word and removes it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With