# Macro magic for string manipulation

**URL:** <https://bnd.discourse.group/t/macro-magic-for-string-manipulation/67>\
**Category:** GENERAL\
**Created:** [January 7, 2021, 12:59pm UTC](https://bnd.discourse.group/t/macro-magic-for-string-manipulation/67 "2021-01-07T12:59:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![juergen-albert](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/juergen-albert/32/20_2.png) [@juergen-albert](https://bnd.discourse.group/u/juergen-albert)\
**Post date:** [January 7, 2021, 12:59pm UTC](https://bnd.discourse.group/t/macro-magic-for-string-manipulation/67/1 "2021-01-07T12:59:54Z")

</div>

Hi,

I want to automatically create a MavenBndRepository Index file with GAV coordinates for a template. I can extract the maven dependencies and replace it in a template.

The extraction of the GAVs looks as follows:

```auto
mavendeps: ${sjoin;\n; ${-maven-dependencies}}

```

with the following result:

```auto
com.sun.activation:javax.activation:1.2.0;groupId="com.sun.activation";artifactId="javax.activation";version="1.2.0";scope=compile
com.sun.xml.bind:jaxb-impl:2.3.3;groupId="com.sun.xml.bind";artifactId=jaxb-impl;version="2.3.3";scope=compile
jakarta.activation:jakarta.activation-api:1.2.1;groupId="jakarta.activation";artifactId="jakarta.activation-api";version="1.2.1";scope=compile
jakarta.annotation:jakarta.annotation-api:1.3.5;groupId="jakarta.annotation";artifactId="jakarta.annotation-api";version="1.3.5";scope=compile

```

I now need to cut of the `;groupId="jakarta.activation";artifactId="jakarta.activation-api";version="1.2.1";scope=compile` part. I’m pretty sure this should be doable with some macro magic, but my attempts till now are in vain.

Somehow the foreach macro is the key, but as I need to cascade a split and sublist macro in it, I don’t see how.

Anybody any idea?

---

<div class="post-metadata">

**Author:** ![pkriens](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/pkriens/32/3_2.png) [@pkriens](https://bnd.discourse.group/u/pkriens)\
**Post date:** [January 7, 2021, 2:21pm UTC](https://bnd.discourse.group/t/macro-magic-for-string-manipulation/67/2 "2021-01-07T14:21:53Z")

</div>

Look at [${template}](https://bnd.discourse.group/t/use-of-the-template-macro/54/2). It was designed for this. You don’t read my tips? 🙂 🙂

```
${template;-maven-dependencies;${@}}
```

---

<div class="post-metadata">

**Author:** ![juergen-albert](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/juergen-albert/32/20_2.png) [@juergen-albert](https://bnd.discourse.group/u/juergen-albert)\
**Post date:** [January 8, 2021, 9:33am UTC](https://bnd.discourse.group/t/macro-magic-for-string-manipulation/67/3 "2021-01-08T09:33:57Z")

</div>

Thanks heaps! That did the trick. I had a vague memory, that something like this exists. Now you told me, I know where it came from.

---

<div class="post-metadata">

**Author:** ![pkriens](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/pkriens/32/3_2.png) [@pkriens](https://bnd.discourse.group/u/pkriens)\
**Post date:** [January 8, 2021, 9:47am UTC](https://bnd.discourse.group/t/macro-magic-for-string-manipulation/67/4 "2021-01-08T09:47:38Z")

</div>

The format of the OSGi headers does rather nastily conflict with the macro’s use of the the semicolon (’;’) to separate its arguments. It used to be _much_ worse since the arguments were expanded before a macro was parsed.

I recently changed to expand them per argument, keeping the macro argument boundaries. So today you could also use the different replacement/substitution macros to extract the _first_ part. That said, the template macro is a lot easier.
