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...
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.
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.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
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.";
}
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.)
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.)