Search code examples
regexasp-classicvbscript

Split a string according to a regexp in VBScript


I would like to split a string into an array according to a regular expression similar to what can be done with preg_split in PHP or VBScript Split function but with a regex in place of delimiter.

Using VBScript Regexp object, I can execute a regex but it returns the matches (so I get a collection of my splitters... that's not what I want)

Is there a way to do so ?

Thank you


Solution

  • If you can reserve a special delimiter string, i.e. a string that you can choose that will never be a part of the real input string (perhaps something like "#@#"), then you can use regex replacement to replace all matches of your pattern to "#@#", and then split on "#@#".

    Another possibility is to use a capturing group. If your delimiter regex is, say, \d+, then you search for (.*?)\d+, and then extract what the group captured in each match (see before and after on rubular.com).