![]() |
|
go ahead... be a heretic | |
PerlMonks |
Re: Get a known substring from a stringby johngg (Canon) |
on Sep 09, 2016 at 21:29 UTC ( #1171486=note: print w/replies, xml ) | Need Help?? |
As BrowserUk has pointed out, it is a little puzzling why you need to search for the ID if you already know it. However, if you are looking for an exact substring within a longer string then index might be a better approach rather than a regex. If you are also wanting to remove the substring from the string then the four argument form of substr is useful as it returns the removed text.
index returns -1 if the substring is not found.
I get a whole load of ID numbers come in from different sources, but for some reason, they aren't spaced apart If the IDs are all mashed together beware of finding false positives. Given 4-digit IDs of 3819, 8076 and 7204 in the string 381980767204, looking for ID 6720 would falsely report as being present. If you are lucky enough to have fixed length IDs, consider breaking the string down using unpack to place the IDs into a hash. Then searching for any ID becomes simple.
I hope this is helpful. Cheers, JohnGG
In Section
Seekers of Perl Wisdom
|
|