Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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.

In reply to String matching and Regular Expressions by root

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-03-28 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found