in reply to REGEX Help

Hi, Try this,

$str="Olympus xModel 3.1mp";
$str =~ s/(.+)mp/$1/g;
print $str;

$str="Olympus yModel 3x Zoom Optical 5x Zoom Digital";
$str =~ s/([0-9]+)x Zoom//g;
print $str;

Sriram

Replies are listed 'Best First'.
Re^2: REGEX Help
by dsheroh (Monsignor) on Jun 24, 2006 at 14:31 UTC
    Your first regex only works in this case because of the quirk in the data that "3.1mp" happens to be at the end of the string. The greediness of (.+) causes it to actually remove the last "mp" in the string, regardless of its context. This also means that if the model is just "3.1" instead of "3.1mp", it will remove the "mp" in "Olympus".

    The second regex is more reliable, but would get false positives if they released a line of cameras with "Super Duper T101x Zoominess(tm)", turning it into "Super Duper Tiness(tm)". It also leaves two consecutive spaces when removing a zoom specification. (Both of these can be fixed by adding a space at the beginning of the regex.)