Carrajo2012-03-22 22:34:46

Ideally a user would enter something like

10 x 12 x 12

but they might enter something like

23x 8x 15

123 x 120x 45

Can someone help me with the regular expression to extract this data?

Thanks,

Lars2012-03-22 22:34:59

Hi

First make a function that adds spaces between when characters comes after a

digit. But this what id the character is a variable. Then

123 x 120x 45

YOu should use * in stead if x as multiplies. That is if you’re trying to do

a calculator.

Or better: If you’re using Linux use Lexecaly Analyser (Lex) and Yet ANother

Compilers Compiler (YACC). An example

Lex easaly helps you skip blanks or whatever. (less that one A4 page)

YACC easaly sets up the syntax for your caclulator (less that one A4 page).

Yours

Lars

“carrajo”

Carrajo2012-03-22 22:35:01

Sorry, I’m trying to extract data from a line of text

Example:

The dimensions are 10 x 12 x 12 for the item whatever……

sometimes the dimensions appear like

23x 8x 15

123 x 120x 45

Can someone help me with the regular expression to extract this data?

Thanks,

Hoss2012-03-22 22:35:03

You could use preg_match_all with the following regular expression (it

just extracts the numbers from a string and puts them in an array)

(\d+)

preg_match_all(‘/(\d+)/’, ’23x 8x 15′,$matches);

Are you only interested in extracting the numbers, or is it important

to get the ‘x’ too? If it is dimensions, then it will always have

three (non-negative) numbers in some form, the expression I posted

should work (UNLESS they enter something like ‘12.1 x5.2 x 5’)

You could try

preg_match_all(‘/([0-9.]+)/’, ‘23.5x 8x 15’,$matches);

which will work for decimals, but it will fail if your string has

periods in it that are not decimal points ‘12.5 x. 1.2. x.’

Lars2012-03-22 22:35:06

Hi

It should be easy to extract digits and characters, some thing like.

//String Numbers[]
//String Mulitpliers[]
//S is the string that contains the sequence of digits and operators.

$i=0;

$n=0;

while($S[i] != null)

{

switch($S[i])

{

case ‘0’,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′: $Numbers[n] =

$Numbers[n] + $S[i];

case ‘x’,’*’: $Mulitpliers[n] = $Mulitpliers[n] + $S[i];

case ‘ ‘: $n = $n+1;

}

$i = $i+1;

}

Lars

“Hoss”

news:c9c2f35a-9585-46ec-aa76-36bb0cf05875@h11g2000prf.googlegroups.com…

You could use preg_match_all with the following regular expression (it

just extracts the numbers from a string and puts them in an array)

(\d+)

preg_match_all(‘/(\d+)/’, ’23x 8x 15′,$matches);

Are you only interested in extracting the numbers, or is it important

to get the ‘x’ too? If it is dimensions, then it will always have

three (non-negative) numbers in some form, the expression I posted

should work (UNLESS they enter something like ‘12.1 x5.2 x 5’)

You could try

preg_match_all(‘/([0-9.]+)/’, ‘23.5x 8x 15’,$matches);

which will work for decimals, but it will fail if your string has

periods in it that are not decimal points ‘12.5 x. 1.2. x.’

Lars2012-03-22 22:35:08

Some correctins.

//String Numbers[]
//String Operators[]
//S is the string that contains the sequence of digits and operators.

$i=0;

$n=0;

while($S[i] != null)

{

switch($S[i])

{

case ‘0’,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′.’0′: $Numbers[n] =

$Numbers[n] + $S[i];

case ‘x’,’*’: $Operators[n] = $Operators[n] + $MULTIPLIER;

case ‘/’: $Operators[n] = $Operators[n] + $DIVISOR;

case ‘+’: $Operators[n] = $Operators[n] + $PLUSS;

case ‘-‘:

{

// check for negative numbers.

if ($i>0)

{

if (S[i-1] == ‘-‘ )

{

$Numbers[n] = ‘-‘ + $Numbers[n]:

}

}

else

{

$Operators[n] = $Operators[n] + $MINUS;

}

}

}

$i = $i+1;

}

NOTE

I haven’t tested the code.

Lars

The “code” I wrote will work since it oly looks at the numbers and

multipliers and skip all the rest.

Noname2012-03-22 22:35:15

$line = ’10 x 12 x 12′;

preg_match(‘/([\d]+)[\sx]+([\d]+)[\sx]+([\d]+)/i’, $line, $match);

print $match[1]; // 10

print $match[2]; // 12

print $match[3]; // 12

Carrajo2012-03-22 22:35:44

Thank you.

## Leave a Reply

You must be logged in to post a comment.