Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

A couple simple perl compatible regular expressions using php's preg_replace function.

Part 1

Okay, I'm trying to wipe out all dollar values from a string. It's driving me insane. I have no idea what I'm doing...

I tried..  $html = preg_replace("\$[0-9]+.[0][0]",'',$html);

Please help!

<td>PKG</td><td>Sport Lite Package (1 @ $254.00)</td><td align="right">$254.00</td>

Part 2.

$4,543.00    AVS85X17DS2  
    $4,917.00    AVS85X17DS2  
    $4,543.00    AVS85X17DS2  
    $21,896.00    EG85X28WT4  
    $6,390.00    KLS7X20WT3  
    $4,723.00    RT7X16WT2  

I'm looking to extract sizes from the top... I don't exactly know what to do... but here are the real values I'm trying to extract

85X17
85X17
85X17
85X28
7X20
7X16

It seems like the sizes are surrounded by alpha.
0
MeridianManagement
Asked:
MeridianManagement
  • 2
  • 2
  • 2
  • +1
3 Solutions
 
MasonWolfCommented:
The period has to be escaped, and you need slashes around the expression:

$html = preg_replace("/\$[0-9]+\.[0-9]{2}/",'',$html);

For your next task, use:
preg_match_all("/[0-9]+X[0-9]+/",$text,$matches);

Your sizes will be contained inside the array $matches[0]

0
 
ozoCommented:
If you have , thousand separators in your numbers
\$[0-9]{1,3}(,[0-9]{3})*\.[0-9]{2}
0
 
MeridianManagementAuthor Commented:
Great, but I can't get the second part to work... I'll keep trying.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
MasonWolfCommented:
"$text" in the second statement is supposed to be the string containing your data including the data about sizes.

Thanks for the clarification ozo, I'd forgotten about the thousands separators.
0
 
mjcoyneCommented:
For the second part, if the patern is always:

<some letters><some digits><the capital letterX><some digits><some letters>

and all you want is the central <some digits><the capital letterX><some digits> part, you can use /(\d+X\d+)/:, as in:

#!/usr/bin/perl -w
use strict;

while (<DATA>) {
    /(\d+X\d+)/;
    print "$1\n";
}

__DATA__
$4,543.00    AVS85X17DS2  
    $4,917.00    AVS85X17DS2  
    $4,543.00    AVS85X17DS2  
    $21,896.00    EG85X28WT4  
    $6,390.00    KLS7X20WT3  
    $4,723.00    RT7X16WT2
0
 
mjcoyneCommented:
The output of the above script, when run against your sample data as indicated, is:

85X17
85X17
85X17
85X28
7X20
7X16
0
 
MeridianManagementAuthor Commented:
Thanks everyone! What was actually wrong was the quotation. Php doesn't like double quotes for preg functions. Great job!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now