Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > Regular Expression Help
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 22nd March 23:34
carrajo
External User
 
Posts: 1
Default Regular Expression Help


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,
  Reply With Quote


  sponsored links


2 22nd March 23:34
lars
External User
 
Posts: 1
Default Regular Expression Help


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 ****yser (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" <carrazola@gmail.com> skrev i meddelandet news:e1163b03-ea7b-4797-a730-b2f2b7316451@q33g2000hsh.googlegroups.com...
  Reply With Quote


  sponsored links


3 22nd March 23:35
carrajo
External User
 
Posts: 1
Default Regular Expression Help


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,
  Reply With Quote
4 22nd March 23:35
hoss
External User
 
Posts: 1
Default Regular Expression Help


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.'
  Reply With Quote
5 22nd March 23:35
lars
External User
 
Posts: 1
Default Regular Expression Help


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" <komeatshield@yahoo.com> skrev i meddelandet
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.'
  Reply With Quote
6 22nd March 23:35
lars
External User
 
Posts: 1
Default Regular Expression Help


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.
  Reply With Quote
7 22nd March 23:35
noname
External User
 
Posts: 1
Default Regular Expression Help


$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
  Reply With Quote
8 22nd March 23:35
carrajo
External User
 
Posts: 1
Default Regular Expression Help


Thank you.
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666