Important alert: (current site time 7/15/2013 8:31:44 PM EDT)
 

article

Checking for the existence of substrings (Updated)

Email
Submitted on: 1/29/2001 10:09:29 AM
By: Brandon McPherson 
Level: Beginner
User Rating: By 7 Users
Compatibility: Active Perl specific
Views: 18728
author picture
(About the author)
 
     This is the most basic example of substring searching I could come up with.... and since I'm a VB programmer for the mostpart, simple is good :)

I've only tried this in ActivePerl, so I won't guarantee it works anywhere else without modifications.

This has been updated in light of the excellent advice I've been given from the more experienced Perl programmers (Thanks PaC).

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

#!/usr/bin/perl
# regular expression matching
use warnings;
use strict;
my $searchstring = "This is a simple demo for
checking keywords in a string.";
# Note: this is pretty simple.
# It's no wonder why people are using Perl for
# their CGI search utilities on the web!
# This little block means 'Print this out
# until you see 'EOF'.
print<<EOF;

Search utility
The string being searched is:
This is a simple demo for checking keywords
in a string.
EOF
if(index $string >= 0) {
print "\n\nNope. The word 'simple' isn't
in there.";
} else {
print "\n\nYup. 'Simple' is in there.";
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/30/2001 7:47:56 AMPaC

you shouldn't use regex for this.

if(index $string >= 0)

is what you want to use, only use regexp matching in an if statement when you're gonna be using extensive matching.
(If this comment was disrespectful, please report it.)

 
1/31/2001 6:19:46 PMBrandon McPherson

Thanks for the tip. Why is regexp matching not considered good usage here?
(If this comment was disrespectful, please report it.)

 
2/1/2001 5:07:58 AMPaC

Brandon, because it's slower to use regex in this case.
index is faster
(If this comment was disrespectful, please report it.)

 
2/9/2001 12:55:54 PMPaC

your welcome Brandon.
if you an IRC client stop by tsunami.dal.net and join #perl
later
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.