tags:

views:

331

answers:

8

I am trying to learn regex. I had the string:

$x = "5ft2inches";

How can I only read [5,2] into an array using regex?

Thanks

+1  A: 

The regular expression is simply /[0-9]+/ but how to get it into an array depends entirely on what programming language you're using.

VoteyDisciple
A: 

You don't mention the language you are using, so here is the general solution: You don't "extract" the numbers, you replace everything except numbers with an empty string.

In C#, this would look like

string numbers = Regex.Replace(dirtyString, "[^0-9]", "");
Abtin Forouzandeh
This doesn't fit his provided example; it would produce "52", meaning that you could not afterward determine if the original string had two integers (5 and 2) or just one (52).
Ben Blank
Ben - you're right. My bad.
Abtin Forouzandeh
+2  A: 

In Perl:

#!/usr/bin/perl

use strict;
use warnings;

my $x = "5ft2inches";
my %height;
@height{qw(feet inches)} = ($x =~ /^([0-9]+)ft([0-9]+)inches$/);

use Data::Dumper;
print Dumper \%height;

Output:

$VAR1 = {
          'feet' => '5',
          'inches' => '2'
        };

Or, using split:

@height{qw(feet inches)} = split /ft|inches/, $x;
Sinan Ünür
Upvoting this and the PHP answer, because not many other languages prefix their variables with `$`.
Chris Lutz
+4  A: 

If you are assuming that the string will be of the form "{number}ft{number}inches" then you can use preg_match():

preg_match('/(\d+)ft(\d+)inches/', $string, $matches);

(\d+) will match a string of one or more digits. The parentheses will tell preg_match() to place the matched numbers into the $matches variable (the third argument to the function). The function will return 1 if it made a match, of 0 if it didn't.

Here is what $matches looks like after a successful match:

Array
(
    [0] => 5ft2inches
    [1] => 5
    [2] => 2
)

The entire matched string is the first element, then the parenthesized matches follow. So to make your desired array:

$array = array($matches[1], $matches[2]);
yjerem
You should use `preg_match_all` and just use the regex `/(\d+)/` in order to handle arbitrary input.
Chris Lutz
+1 for correctly guessing the language.
Sinan Ünür
+1  A: 

With Regular Expressions, you can either extract your data in a contextless way, or a contextful way.

IE, if you match for any digits: (\d+) (NB: Assumes that your language honors \d as the shortcut for 'any digits')

You can then extract each group, but you might not know that your string was actually "5 2inches" instead of "6ft2inches" OR "29Cabbages1Fish4Cows".

If you add context: (\d+)ft(\d+)inches

You know for sure what you've extracted (Because otherwise you'd not get a match) and can refer to each group in turn to get the feet and inches.

If you're not always going to have a pair of numbers to extract, you'll need to make the various components optional. Check out This Regular Expression Cheat Sheet (His other cheat sheets are nifty too) for more info,

Dylan Lacey
A: 

Have to watch out for double digit numbers.

/\d{1,2}/

might work a little better for feet and inches. The max value of '2' should be upped to whatever is appropriate.

Rob
A: 
use `/[\d]/`
nandocurty
+1  A: 

Assuming PHP, any reason no one has suggested split?

$numbers = preg_split('/[^0-9]+/', $x, -1, PREG_SPLIT_NO_EMPTY);
thorncp
It would be better to split on what you **don't** want. See my answer (albeit in Perl).
Sinan Ünür
[^0-9]+ is everything that isn't a number, which is what we don't want.
thorncp