Split String By Char, But Skip Certain Char Combinations
Solution 1:
Try with this C# solution, it uses positive lookbehind and positive lookahead:
string s = @"First/Second//Third/Fourth";
var values = Regex.Split(s, @"(?<=[^/])/(?=[^/])", RegexOptions.None);
It says: delimiter is /
which is preceded by any character except /
and followed by any character except /
.
Here is another, shorter, version that uses negative lookbehind and lookahead:
var values = Regex.Split(s, @"(?<!/)/(?!/)", RegexOptions.None);
This says: delimiter is /
which is not preceded by /
and not followed by /
You can find out more about 'lookarounds' here.
Solution 2:
In .NET Regex you can do it with negative assertions.(?<!/)/(?!/)
will work. Use Regex.Split
method.
Solution 3:
ok one thing you can do is to split the string based on /
. The array you get back will contain empty allocations for all the places //
were used. loop through the array and concatenate i-1
and i+1
allocations where i
is the pointer to the empty allocation.
Solution 4:
How about this:
vararray = "First/Second//Third/Fourth".replace("//", "%%").split("/");
array.forEach(function(element, index) {
array[index] = element.replace("%%", "//");
});
Post a Comment for "Split String By Char, But Skip Certain Char Combinations"