How do I enclose all numeric substrings of a string in braces?

perl, regex

Solution

This is for sure possible.

You want a regex that matches on digits, captures the digits, then substitutes the match with a wrapped set of curly braces.

my $input = 'APPL[E]5XXXX';
$input =~ s/(\d+)/{$1}/g;

- The `\d+` matches digits.

- The `()` captures and stores in `$1`.

- The `s///` is a substitution regex.

- And finally, the `/g` at the end means 'global', aka, do this for all matches.

Problem

I would like to convert a string so that all numeric subsequences are enclosed in a `{`...`}` pair. For instance: ``` input_string = "APPL[E]5XXXX" output_string = "APPL[E]{5}XXXX" ``` Each string may contain one or more digits, for instance `BASIC76XXXXX98ZZZZ` and output should be `BASIC{76}XXXXX{98}zzzz` Not sure if this possible to achieve. Any help will be very much appreciated.

Original source