Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

String matching and Regular Expressions

by root (Scribe)
on Nov 10, 1999 at 03:50 UTC ( #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!
Re: String matching and Regular Expressions
by kallol.chakra (Initiate) on Aug 29, 2009 at 20:44 UTC
    Hi, I have a doubt about this. Lets take this scenario. The Input data is as follows: header { subheader1 { field1 : value field2 : value field3: value } subheader2 { subheader3 { field4 : value field5 : value field6: value } subheader4 { field7 : value field8 : value field9: value } } } header { subheader1 { field1 : value1 field2 : value1 field3: value1 } subheader2 { subheader3 { field4 : value1 field5 : value1 field6: value1 } subheader4 { field7 : value1 field8 : value1 field9: value1 } } } The output will be : field1,field2,field3,field4,field5,field6,field7,field8,field9 value,value,value,value,value,value,value,value,value value1,value1,value1,value1,value1,value1,value1,value1,value1 Here main problem is that: The value os field2 or field3 may be missing then the output may be: field1,field2,field3,field4,field5,field6,field7,field8,field9 value,value,value,,value,value,value,value,value value1,value1,value1,value1,value1,value1,,value1,value1 In the above output The first line field4 is missing and in the second line field7 is missing How Can I get the output can you please help me.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://962]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2017-06-25 10:49 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (567 votes). Check out past polls.