Skip to content
Advertisement

Split string on non-alphanumeric characters and on positions between digits and non-digits

I’m trying to split a string by non-alphanumeric delimiting characters AND between alternations of digits and non-digits. The end result should be a flat array of consisting of alphabetic strings and numeric strings.

I’m working in PHP, and would like to use REGEX.

Examples:

  • ES-3810/24MX should become ['ES', '3810', '24', 'MX']
  • CISCO1538M should become ['CISCO' , '1538', 'M']

The input file sequence can be indifferently DIGITS or ALPHA.

The separators can be non-ALPHA and non-DIGIT chars, as well as a change between a DIGIT sequence to an APLHA sequence, and vice versa.

Advertisement

Answer

The command to match all occurrances of a regex is preg_match_all() which outputs a multidimensional array of results. The regex is very simple… any digit ([0-9]) one or more times (+) or (|) any letter ([A-z]) one or more times (+). Note the capital A and lowercase z to include all upper and lowercase letters.

The textarea and php tags are inluded for convenience, so you can drop into your php file and see the results.

JavaScript

Which outputs in the textarea:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement