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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having difficulty constructing regular expression IF 2

Status
Not open for further replies.

Kendertaunt

Programmer
Dec 3, 2001
21
US
Hello!

I have tried so hard to do this on my own, now it's time to admit that I do not understand and see if someone can help! I did read FAQs on regular expressions, including goBoating's FAQ here on Tek-Tips. I think I am confused as to how to translate what I'm learning to my own example (which contains literal slashes - something else that's making me confused!)

Here's the thing. I'm pulling in a web site, getting it with no problem. I need to check the value of <img SRC>, which could be one of a few choices:

img src=&quot;/icons/sample1.gif&quot;
img src=&quot;/icons/sample2.gif&quot;

Basically, to make a short story long, :), how do I test for the following:

/icons/sample[ANY NUMERIC CHARACTER].gif

I understand this is a really basic manuever in Perl. Believe me, I've tried everything. I think it has something to do with the fact that I'm not dealing with the fact that there are already literal slashes in there.

Here's some of the things I've tried:

if ($token->[1]{src} eq '/icons/sample[0-9]) {

or

if ($token->[1]{src} eq \/icons\/sample[\d]\.gif) {

or

my $IsIcon =~ \/icons\/sample(0-9)\.gif$/i ;
if ($token->[1]{src} eq $IsIcon) {

If someone could help me, I would LOVE to understand why everything I'm trying is just not working! It's not passing the If test.
If I hardcode it, it's fine. For example, this works:

if ($token->[1]{src} eq '/icons/sample1.gif') {

Thanks!
Jess :)
 
One more thing...
I've also tried this:

if ($token->[1]{src} eq '/icons/sample'.[0-9].'.gif') {

Thanks,
Jess :)
 
try
$match = &quot;/aaa/bbb/ccc\d+.gif&quot;;

if( $myicon =~ $match) ...
vox clamantis in deserto.
 
The 'eq' is a string test operator. In other words, it checks that strings are exactly the same. Since you want to match multiple strings, you can't use 'eq'. You need to use a regular expression to match the string '/icons/sample[ANY NUMERIC CHARACTER].gif'. In particular you want to use the pattern matching operator 'm//'. With the pattern matching operator, you can use other quoting operators instead of '/'. Note that you have to include the 'm' if you don't use '/' as the quoting operator. Since your string contains '/', lets choose '|' as the quoting operator.

if ($token->[1]{src} =~ m|/icons/sample\d|) {

The '\d' matches any digit. This also can be written as '[0-9]'.



 
Thanks for the response!
Unfortunately, that did not work. It passed the syntax check and ran, but did not pass the test. It did not get past the If.

Jess :)
 
RAIDER!

You made my day. :)

And, thank you very much for explaining why what I was trying did not work. I also did not know that you can use a different delimiter for Matches.

Wonderful, wonderful, wonderful!
Jess
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top