Jim Miller
Jim Miller

Reputation: 3329

Please confirm a regex for stripping an optional number off the end of a string?

I have some strings that may or may not have a number at the end, like so:

THIS IS A STRING
THIS IS ALSO A STRING      123

That is, I'm trying to get the text content of the string, without the optional whitespace and number at the end of the string. Since I'm in php, I'll be using preg_match to capture the text in front of the spaces-and-number, thus the capture group around the initial content. There are no constraints on the amount of whitespace and the number of digits in the number; it can be assumed that the number, if it is present, is at the end of the string.

The following seems to work for me:

/^(.*?)(\s*\d*?)?$/i

but there are surely people out there with more wisdom about regex than I. Any suggestions? Thanks!

Upvotes: 0

Views: 41

Answers (3)

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21681

I would just use

  /(\s+\d+)?$/

the \s+ or \s* depends if you want to remove numbers from the end of a word like this THIS IS ALSO A STRING123 With + it wont remove them and it only removes "free" numbers like this: THIS IS ALSO A STRING 123. It's not clear in the question which is acceptable.

Test It

The only reason the capture group is needed is because the \s should only be replaced if there were numbers too (maybe).

Something that made me think of is you could even do this \d*$|\s*$ but the order is important, because first it will remove \d then it will trim the end. And the next logical thing would be to do ^\s*|\d*$|\s*$ and trim both sides. Now you don't need to do trim on it.

Test That

With preg_replace there is no need to capture and replace the beginning of the string, you only need to match what you want to replace.

UPDATE

Here is a full up example

//replaces only free numbers
$str = 'THIS IS ALSO A STRING      123';
echo preg_replace('/(\s+\d+)?$/', '', $str); 
//output: 'THIS IS ALSO A STRING'

//does not trim
$str = '   THIS IS ALSO A STRING      123   ';
echo preg_replace('/(\s+\d+)?$/', '', $str); 
//output: '   THIS IS ALSO A STRING   '

//does NOT strip numbers from the end of a word
$str = 'THIS IS ALSO A STRING123';
echo preg_replace('/(\s+\d+)?$/', '', $str); 
//output: 'THIS IS ALSO A STRING123'

//same as above except DOES strip numbers from the end of a word
$str = 'THIS IS ALSO A STRING123';
echo preg_replace('/(\s+\d+)?$/', '', $str); 
//output: 'THIS IS ALSO A STRING'

//same as the last one except trims both sides
$str = '   THIS IS ALSO A STRING123   ';
echo preg_replace('^\s*|\d*$|\s*$', '', $str); 
//output: 'THIS IS ALSO A STRING'

//using preg_match
$str = 'THIS IS ALSO A STRING   123';
echo preg_match('/(.+?)(\s*\d+)?$/', $str, $match); 
echo $match[1];
//output: 'THIS IS ALSO A STRING'
echo $match[2];
//output: '   123'

//same as above except also captures the numbers without whitespace
//wont match 'THIS IS ALSO A STRING   123  ' <-- ending white space
$str = 'THIS IS ALSO A STRING   123';
echo preg_match('/(.+?)(?:\s*(\d+))?$/', $str, $match); 
echo $match[1];
//output: 'THIS IS ALSO A STRING'
echo $match[2];
//output: '123'

//same as above except adds something like trimming
$str = '  THIS IS ALSO A STRING   123  ';
echo preg_match('/\s*(.+?)(?:\s*(\d+))?\s*$/', $str, $match); 
echo $match[1];
//output: 'THIS IS ALSO A STRING'
echo $match[2];
//output: '123'

Hope that helps, it really depends on how flexible you need to be and "exactly" what you want to do.

Upvotes: 1

Mihai Tintea
Mihai Tintea

Reputation: 1

You may use this regex:

[[:blank:][:digit:]]*$

Upvotes: -1

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41820

It looks like just using rtrim could also be a possibility.

$string = rtrim($string, ' 0123456789');

Should be fine unless the end of the string before the spaces/digits ends in a number, this would strip that off as well.

Upvotes: 1

Related Questions