http://qs321.pair.com?node_id=962

Part of what makes Perl such a useful language is its powerful string-matching and handling capabilities. Regular expressions are basically patterns a programmer can compare a string of text to. Matching a regular expression with a string of text either returns true or false. The two main pattern matching operators are m// and s//. These are the matching and substitution operators respectively. Another function that makes use of regular expressions is split The matching operator m// is normally written //. Perl allows you to change the delimiters to something besides /. If you don't change the delimiters from /, you can use // instead of m//. Now for a quick example of m//:
while(<>){ if(/the/){ #does $_ contain the print "Your line of text contains the word 'the'\n"; } }

Now for a quick example of s///:
@machines_os=("OpenBSD","Windows","Linux", "Windows"); foreach(@machines_os){ s/Windows/Linux/; }

This function goes through each of the items in @machine_os. If any of them contain Windows, the thing between the first set of //. It is replaced with Linux the string between the second and third /'s. You can see why you've gotta love Perl. Instead of 1 OpenBSD machine, 2 Windows machines and a Linux box, I now have 1 OpenBSD box and 3 Linux machines. At least that is what @machines_os now contains.

Now on to Quantifiers in regular expressions.

Replies are listed 'Best First'.
RE: String matching and Regular Expressions
by neophyte (Curate) on Nov 02, 2000 at 14:48 UTC
    My service for German language perl beginners ;-)
    While Mastering Regular Expressions is available in translation you might want to have a first short look at what RegEx can do for you in a short German article.
    You can find that here: Reguläre Ausdrücke in Perl.
    Hope this helps.

    neophyte

Pattern Matching:#2
by Anonymous Monk on Mar 23, 2000 at 01:29 UTC
    You don't explain in the tuturial what the trailing /i does, hardly seems fair to quiz on it. There should be a section detailing what /i or /g does trailing the reg expr. I looked and found nothing about it.
      The trailing "i" means it ignores the case.
      The "g" is the global replace option.

      There's a good example of "i" in Learning Perl 2nd edition.
      On page 11 and the program continues on page 12.
      It's really helpful for making your programs dummy proof for users.

      An example of "g" can be found on page 88 of the same book.
      It is my bible although I have two other books I am using.
      (a programming newbie can never have enough books)

      I almost forgot to mention that issuing the command "perldoc perlop"
      (without the quotes) on most systems will give you
      detailed information on perl operators.

      Perldoc is excellent! use "perldoc perldoc" to learn how to use it.
      Another really good book is Mastering Regular Expressions. Not only does it give easy to understand detailed and insightful information on regular expressions, it also suggests having a beer while your reading the book. ;-)
Re: String matching and Regular Expressions
by iza (Monk) on Mar 14, 2002 at 13:10 UTC
    even after reading the tutorial and the perlman:perlre page, i still don't understand why everywhere it is said to use s/// or m//, and every example use ~s/// or ~m// - i mean, i don't understand where this ~ comes from and what it means ...
      It's not ~s and ~m but =~. See perlop for details.

      rdfield

Re: String matching and Regular Expressions
by lvanhout (Curate) on Jun 20, 2004 at 19:59 UTC
    In the Exercises the solution to number 1 has an error.
    13: print $text; #write changes to file should be 13: print FILE $text; #write changes to file
    I did this one and the solution just prints to screen but leaves the file empty.

    Lane
Re: String matching and Regular Expressions
by boxer (Initiate) on Apr 26, 2002 at 15:27 UTC
    Need Help! How would I match a user input (password) to a database and halt the user is it does not match or allow the user in if it does. School project is hung up on this.
Re: String matching and Regular Expressions
by Perlin_the_Red (Initiate) on Sep 16, 2002 at 18:05 UTC
    I'm having a wicked time setting up a pattern for the following: I have a text doc that contains info like the following: <space> \\dir1\<return> group\name1<return> group\name2<return> group\name3<return> <space> \\dir2\<return> group\name4<return> group\name5<return> group\name6<return> <space> I need to go through the list and find the "group\name" I am searching for (using $search as a variable), dump all the info pertaining to where that group is to a file handle. For ex: If I am searching for "group\name5" , I want to return the following info: \\dir2\<return> group\name4<return> group\name5<return> group\name6<return> I've setup a pattern that looks for /^\s+.*($search).*\s+$/ using the /si at the end for any case and including line returns, but it never matches anything up. What is the secret with the /s and multiple input lines? Thanks!
A reply falls below the community's threshold of quality. You may see it by logging in.