Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

String matching and Regular Expressions

by root (Monk)
on Nov 10, 1999 at 03:50 UTC ( [id://962]=perltutorial: print w/replies, xml ) Need Help??

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.


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.


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.

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://962]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-06-18 18:13 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.