Regex: Divide string by number / string?

Consider the following:

700italic regular 300bold 300bold900 

These are all different examples, only one of the lines will be executed in time.

Expected Result:

 // 700italic array( 0 => 700 1 => itailc ) // regular array( 0 => regular ) // 300bold array( 0 => 300 1 => bold ) // 300bold900 array( 0 => 300 1 => bold 2 => 900 ) 

I have done the following:

 (\d*)(\w*) 

But this is not enough. This works when I have only two โ€œpartsโ€ (number | string or string | number), but if I add a third โ€œsegmentโ€ to it, I will not work.

Any suggestions?

+4
source share
6 answers

You can use preg_split instead. Then you can use images that match the position between the word a letter:

 $result = preg_split('/(?<=\d)(?=[az])|(?<=[az])(?=\d)/i', $input); 

Note that \w also matches numbers (and underscores) in addition to letters.

An alternative (using the match function) is to use preg_match_all and match only numbers or letters for each match:

 preg_match_all('/\d+|[az]+/i', $input, $result); 

Instead of captures, you will get one match for each of the desired elements in the resulting array. But in the end, you need an array, so you don't care where they came from.

+5
source

You can use the flag PREG_SPLIT_DELIM_CAPTURE .

Example:

 <?php $key= "group123425"; $pattern = "/(\d+)/"; $array = preg_split($pattern, $key, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); print_r($array); ?> 

Check this post.

+1
source

You are looking for preg_split :

 preg_split( '((\d+|\D+))', $subject, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ) 

Demo

Or preg_match_all :

 preg_match_all('(\d+|\D+)', $test, $matches) && $matches = $matches[0]; 

Demo

+1
source

You should match it, not break it.

However, you can break it using

 (?<=\d)(?=[a-zA-Z])|(?<=[a-zA-Z])(?=\d) 
0
source

You can use a template like this:

 (\d*)([a-zA-Z]*)(\d*) 

Or you can use preg_match_all with a template like this:

 '/(?:[a-zA-Z]+|\d+)/' 

Then you can match an arbitrary number of segments, each of which consists of only letters or only numbers.

0
source

Maybe something like this:

 (\d*)(bold|italic|regular)(\d*) 

or

 (\d*)([a-zA-Z]*)(\d*) 
0
source

All Articles