# how to remove substring/suffix using generator expressions

**URL:** https://discourse.cmake.org/t/how-to-remove-substring-suffix-using-generator-expressions/701
**Category:** Usage
**Created:** [February 25, 2020, 2:31pm UTC](https://discourse.cmake.org/t/how-to-remove-substring-suffix-using-generator-expressions/701 "2020-02-25T14:31:31Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mmoelle1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mmoelle1/32/382_2.png) [@mmoelle1](https://discourse.cmake.org/u/mmoelle1)
#### Post date: [February 25, 2020, 2:31pm UTC](https://discourse.cmake.org/t/how-to-remove-substring-suffix-using-generator-expressions/701/1 "2020-02-25T14:31:31Z")

</div>

Assume that I have the expression “/home/user/foo.bar” as the result of a generator-expression and I would like to remove the suffix “.bar”. In practice, the first expression is the full path to an object file and the second is the suffix, that I need to remove in order to obtain the expression “/home/user/foo” which I can then write to file using

file(GENERATE OUTPUT filename CONTENT “$\<…\>”)

Is it possible to remove substrings within generator expressions?

Many thanks in advance and kind regards,  
Matthias

---

<div class="post-metadata">

### Author: ![Gonzalo\_Garramuno](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gonzalo_garramuno/32/265_2.png) [@Gonzalo\_Garramuno](https://discourse.cmake.org/u/Gonzalo_Garramuno)
#### Post date: [February 26, 2020, 2:47am UTC](https://discourse.cmake.org/t/how-to-remove-substring-suffix-using-generator-expressions/701/2 "2020-02-26T02:47:45Z")

</div>

No. You’ll have to put it in a variable and use STRING( REGEX  
REPLACE …) to strip out the .bar extension.

---

<div class="post-metadata">

### Author: ![mmoelle1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mmoelle1/32/382_2.png) [@mmoelle1](https://discourse.cmake.org/u/mmoelle1)
#### Post date: [February 26, 2020, 5:39am UTC](https://discourse.cmake.org/t/how-to-remove-substring-suffix-using-generator-expressions/701/3 "2020-02-26T05:39:31Z")

</div>

Thanks! In my understanding, generator expressions are evaluated AFTER the CMakeLists.txt file(s) have been processed. How can I “put” the result of a generator expression into a variable that I can then further process using STRING? Thanks in advance.

---

<div class="post-metadata">

### Author: ![Gonzalo\_Garramuno](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gonzalo_garramuno/32/265_2.png) [@Gonzalo\_Garramuno](https://discourse.cmake.org/u/Gonzalo_Garramuno)
#### Post date: [February 26, 2020, 11:03am UTC](https://discourse.cmake.org/t/how-to-remove-substring-suffix-using-generator-expressions/701/4 "2020-02-26T11:03:14Z")

</div>

You are of course correct. Disregard my suggestion. You can  
probably use thegenerator expressions to do what you want.

---

<div class="post-metadata">

### Author: ![Gonzalo\_Garramuno](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gonzalo_garramuno/32/265_2.png) [@Gonzalo\_Garramuno](https://discourse.cmake.org/u/Gonzalo_Garramuno)
#### Post date: [February 26, 2020, 11:05am UTC](https://discourse.cmake.org/t/how-to-remove-substring-suffix-using-generator-expressions/701/5 "2020-02-26T11:05:42Z")

</div>

TARGET\_FILE\_BASE\_NAME:tgt  
or  
TARGET\_FILE\_NAME:tgt  
The email discourse client swallowed these in the previous answer.

---

<div class="post-metadata">

### Author: ![mmoelle1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mmoelle1/32/382_2.png) [@mmoelle1](https://discourse.cmake.org/u/mmoelle1)
#### Post date: [February 26, 2020, 11:13am UTC](https://discourse.cmake.org/t/how-to-remove-substring-suffix-using-generator-expressions/701/6 "2020-02-26T11:13:23Z")

</div>

Thanks but it’s not that easy I guess. I need the base name of a particular object file of a target. It’s the attempt to solve my problem with precompiled headers described in a previous post.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 28, 2020, 8:32pm UTC](https://discourse.cmake.org/t/how-to-remove-substring-suffix-using-generator-expressions/701/7 "2020-02-28T20:32:25Z")

</div>

I suggested other solutions over there, but I think that more path manipulation/extraction genexes are likely useful anyways. A feature request for them would be useful.
