Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression help (preg_match_all)

Status
Not open for further replies.

jfarmerjr

Programmer
Mar 10, 2010
21
US
I need to find all instances of some numbers in a bunch of text files, here's an example pulled from one of the text files:

00 CX76959 01 3/03 Del Rio TX Nogales AZ 768 1.065 185 .810 967.77 03
00 CX76959 01 3/09 FSC Ml: 768 Rate: .2570 197.38 197.38 N 197.38 03
00 CX86185 01 3/08 IndianaplsIN Manteno IL 160 1.065 1 .810 171.21 03
00 CX86185 01 3/10 FSC Ml: 160 Rate: .2570 41.12 41.12 N 41.12 03


I need to pull the price from any line that has FSC. Specifically I need the dollar amount after Rate: .xxxx
From the example, I need to pull out 197.38 and 41.12

I need to pull all the FSC numbers from a text file and total them. What pattern would work?

Thanks
 
I tried an online regex creator which gave me this:

if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8."/is", $FSCtmp, $matches))
{
$word1=$matches[1][0];
$float1=$matches[2][0];
print "($word1) ($float1) \n";
}


{
$word1=$matches[1][0];
$float1=$matches[2][0];
print "($word1) ($float1) \n";
}

but it only shows the first instance on each FSC price.
 
Whoops, forgot some of it. Here it is in it's entirety:

re1='.*?'; # Non-greedy match on filler
$re2='(?:[a-z][a-z]+)'; # Uninteresting: word
$re3='.*?'; # Non-greedy match on filler
$re4='((?:[a-z][a-z]+))'; # Word 1
$re5='.*?'; # Non-greedy match on filler
$re6='[+-]?\\d*\\.\\d+(?![-+0-9\\.])'; # Uninteresting: float
$re7='.*?'; # Non-greedy match on filler
$re8='([+-]?\\d*\\.\\d+)(?![-+0-9\\.])'; # Float 1

if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8."/is", $txt, $matches))
{
$word1=$matches[1][0];
$float1=$matches[2][0];
print "($word1) ($float1) \n";
}


{
$word1=$matches[1][0];
$float1=$matches[2][0];
print "($word1) ($float1) \n";
}
 
Hi

I would use this :
Code:
[COLOR=darkgoldenrod]preg_match_all[/color][teal]([/teal][green][i]'/FSC.*Rate: ([.\d]+)/'[/i][/green][teal],[/teal][navy]$txt[/navy][teal],[/teal][navy]$matches[/navy][teal]);[/teal]
( Examine the $matches with [tt]print_r()[/tt] to see what you get. )

Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.

Feherke.
 
Thanks Feherke,

I'll be sure to use the TGML tags from now on.

That's almost there, but it's capturing the rate and I need to capture the float found after that one, (197.38 and 41.12 from the example text on the first post.)
 
Hi

Oops, I misunderstood the requirement.
Code:
[COLOR=darkgoldenrod]preg_match_all[/color][teal]([/teal][green][i]'/FSC.*Rate: \S+\s+([.\d]+)/'[/i][/green][teal],[/teal][navy]$str[/navy][teal],[/teal][navy]$match[/navy][teal]);[/teal]


Feherke.
 
Works like a charm! Once again, I'd like to say: You're awesome Feherke! Thanks a bunch. Every I've come to this site looking for regex help(1 other time in the AWK forum I believe) you've come to the rescue and I sincerely appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top