Split a string using regular expression

Hello,

I would like to split a string using a regular expression.
the part i want to split is always 1 or more numbers followed by either an optional letter or a “-” (minus) followed by 1 or more numbers.

can anyone help me fix this?
examples:

test 123
test 1a
test 1
test 12-34

part 1 is always "test " and splitting on the space isnt an option because "test " is usually much longer with optional spaces and other characters in it.
part 2 in this example is always the part after "test "

thanks in advance

You will need to show more useful test cases if

part 1 is always "test " and splitting on the space isnt an option because "test " is usually much longer with optional spaces and other characters in it.
Your tests show test always the same, if it isn't then you can specify what it might* be.

[php]
$str = [
‘test 123’,
‘test 1a’,
‘test 1’,
‘test 12-34’
];
$ar = [];

foreach ( $str as $st ){
$pattern = ‘/test\s(\d{0,4}\s?-?\w{0,2})/’;
if(preg_match($pattern, $st, $match)){
array_push($ar, $match[1]);
}
}

echo “

”;
print_r($ar);
echo “
”;

[/php]

This works for your test cases, but that is the pattern you are showing.

Thanks but that won’t work as “part 1” is more variable then just "test ".
Better examples are:

test 123
test test 12a
t.st test 12-34
test-test 1

[php]$str = [
‘test 123’,
‘test test 12a’,
‘t.st test 12-34’,
‘test-test 1’,
];
$ar = [];

foreach ( $str as $st ){
$pattern = ‘/(\d{0,4}\s?-?\w{0,2})$/’;
if(preg_match($pattern, $st, $match)){
array_push($ar, $match[1]);
}
}

echo “

”;
print_r($ar);
echo “
”;
[/php]

Outputs:

Array ( [0] => 123 [1] => 12a [2] => 12-34 [3] => 1 )

thanks a lot! i almost got it right now.

i changed it into:
[php]<?php
//Enter your code here, enjoy!
$str= ‘t.st test 12-23s’;

$pattern = '/(\d{0,4}\s?-?\w{0,2})$/';
if(preg_match($pattern, $str, $match)){
	echo trim(str_replace($match[0], "", $str)) . "\n";
	$pattern2 = '/[a-zA-Z]$/';
	if (preg_match($pattern2, $match[1], $test)){
		echo trim(str_replace($test[0], "", $match[1])) . "\n";
		echo $test[0] . "\n";
	} else {
	    echo  $match[1] . "\n";
	}
}[/php]

the only problem is that the 2nd echo should be 12-23 and the first should not end with 12-.

could you help me solve that properly pls?

This may be more workable then,
[php]$str = ‘t.st test 12-23s’;

$pattern = ‘/(\d{0,4}\s?-?\d{0,4})\w$/’;

if (preg_match ( $pattern, $str, $match )) {
print_r( $match);

}[/php]

Or just use explode() function and take the last entry…

$str = ‘t.st test 12-23s’;
$results = end(explode(" ", $str));

thanks a lot guys. i used this:
[php]$str= ‘t.st test 12-14a’;

$pattern = '/(\d{0,4}\s?-?\d{0,4})\w$/';
if(preg_match($pattern, $str, $match)){
	echo trim(str_replace($match[0], "", $str)) . "\n";
	$pattern2 = '/[a-zA-Z]$/';
	if (preg_match($pattern2, $match[0], $test)){
		echo trim(str_replace($test[0], "", $match[0])) . "\n";
		echo $test[0] . "\n";
	} else {
	    echo  $match[0] . "\n";
	}
}[/php]

Wow, lots of code instead of the one line I gave you…

Sponsor our Newsletter | Privacy Policy | Terms of Service