In the first case it is probably sufficient to ensure that the preceeding character was a digit and that the following character is a non-word character so s/(?<=\d)mp\b//g should do that one.
In the second case it is actually even simpler, no zero width look back assertion required: s/\s+\d+x Zoom//g.
Update: fixed broken regex. Was s/(><=\d)mp\b//g
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |
Thanks a lot.
The second problem got resolved but the first issue still
persists. What could be missing in this solution
$original_text="Olympus xModel 3.1mp";
$original_text=~ s/(><=\d)mp\b//g;
print "\n$original_text";
Regards
Habib | [reply] [d/l] |
The absence of a stupid typo! Sorry, the regex should have been $original_text=~ s/(?<=\d)mp\b//g;.
By the way I strongly recommend that you add use strict; use warnings; to any script you write. You then need to declare variables using my, but many problems get found before they bite hard.
You should also browse the following documentation, probably in the order given: perlretut, perlre and perlreref.
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |
| [reply] [d/l] [select] |
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
| [reply] [d/l] [select] |
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.)
| [reply] [d/l] |