Split a row by group [ALPHA] [DIGIT] [ALPHA]

I am trying to delete a string grouped by type (only ALPHA or DIGIT), without any other character.

I work in PHP and want to use REGEX.

I need to convert an input string of type ES-3810 / 24MX "to an array, for example [ES] [3810] [24] [MX] , and an input string of type" CISCO1538M "to an array such as [CISCO] [1538] [M] .

The sequence of input files may be indifferent to DIGITS or ALPHA.

Separators can be non-ALPHA and non-DIGIT characters, as well as a change between the DIGIT sequence and the APLHA sequence and vice versa.

Sorry for my poor English ... hope my explanation seems clear, however.

Thanks for your great help.

Sincerely.

+4
source share
2 answers

A command to match all occurrences of the regular expression preg_match_all() , which displays a multidimensional array of results. A regular expression is very simple ... any number ( [0-9] ) one or more times ( + ) or ( | ) any letter ( [Az] ) one or more times ( + ). Pay attention to capital A and lowercase z to include all uppercase and lowercase letters.

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

 <textarea style="width:400px; height:400px;"> <?php foreach( array( "ES-3810/24MX", "CISCO1538M", "123ABC-ThatsHowEasy" ) as $string ){ // get all matches into an array preg_match_all("/[0-9]+|[[:upper:][:lower:]]+/",$string,$matches); // it is the 0th match that you are interested in... print_r( $matches[0] ); } ?> </textarea> 

What are the outputs in the text box:

 Array ( [0] => ES [1] => 3810 [2] => 24 [3] => MX ) Array ( [0] => CISCO [1] => 1538 [2] => M ) Array ( [0] => 123 [1] => ABC [2] => ThatsHowEasy ) 
+3
source
 $str = "ES-3810/24MX35 123 TEST 34/TEST"; $str = preg_replace(array("#[^A-Z0-9]+#i","#\s+#","#([AZ])([0-9])#i","#([0-9])([AZ])#i"),array(" "," ","$1 $2","$1 $2"),$str); echo $str; $data = explode(" ",$str); print_r($data); 

I could not think of a cleaner way.

+1
source

All Articles