Question

Given a string, i need to split the string on forward slashes, but only if those forward slashes don't appear in a {} block.

I know this can be accomplished in a variety of other ways. At this point, I just want to see if this is possible and what the regex will look like. And if it is functional, it would likely speed up the program a little bit too. Win win. :)

Using perl in the following examples, though it may ultimately be implemented in another language.

This is essentially what I want to do:

#!/bin/perl -w
use strict;
use Data::Dumper;

my @strings = (
    "/this/that",
    "/this/", 
    "/this/{ID=/foo\/bar/}",
    "/this/{ID=foo/bar}",
    "/this/{/}",
    "/this/{ID=/foobar/}/that/foo/",
    "/this/{ID=f/o/o/b/a/r/}",
    "/this/{ID=/foobar/}/that/{bar}/that"
);


foreach my $string (@strings) {
    print $string."\n";

    my @items = split(/(?<!{.*?)\/(?!.*?})/,$string);

    print Dumper(\@items);

}

The problem is that you can't use a variable length look behind.

So, i've been playing with using only look aheads to accomplish the same thing.

The closest I've been able to come is using this line for the split:

my @items = split(/\/(?![^{].*?}|})/,$string);

That almost gets it, but doesn't split on / before a {} block. So i end up with results like this:

$VAR1 = [
      '/this',
      '{ID=/foobar/}/that',
      '{bar}',
      'that'
    ];

where it should be:

$VAR1 = [
      'this',
      '{ID=/foobar/}',
      'that',
      '{bar}',
      'that'
    ];

Thanks in advance.

Was it helpful?

Solution

You can change your current regex to:

/(?![^{]*\})

It will match a / if there's no } ahead of it.

For example, you will get a split where there are matches here.

But I think that it'd be perhaps easier with a match instead?

\{[^}]*\}|[^/]+

regex101 demo

Now, the above assume there's no nesting of braces within the strings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top