Finding
Previous  Top  Next

Let's look at some examples again to find a string in another string!

1: $text = "Hey, my name is Ingmar and I am 25 years old!";
2:
3: if ($text =~ /my name is/) {
4:   print "Somebody has a name";
5: }

This of course would yield the following output:

Somebody has a name

You probably already saw how a regular expression should look like:

$test =~ /something/;

Please make sure you have no space after the = and that there is a space after the ~. Now what if we want to know if somebody is screaming? We do:

1: $text = "Hey, my name is Ingmar and I am 25 years old!";
2:
3: if ($text =~ /!$/) {
4:   print "Somebody is screaming ...";
5: }

which also would yield

Somebody is screaming ...

That's because the $ in regular expressions refers to the end of the line. One more example to find the age!

1: $text = "Hey, my name is Ingmar and I am 25 years old!";
2:
3: if ($text =~ /\d\d/) {
4:   print "Somebody has a two digit age!";
5: }

which also would yield

Somebody has a two digit age!

You can build your own examples here if you refer to table above. I would actually recommend this since practicing seems to be the only way to really understand (more complex) regular expressions. And that's why we move on to the next chapter.