Back to labs/answer key page

Change this line:

    s/\. /\.\n/g; 

to this:

    s/([\.!\?]) /\1\n/g; 

The input string here is a regular expression: [\.!\?] matches either a period, and exclamation point, or a question mark. (The period and the questionmark are both special characters in regular expressions, so they need to be escaped with \.) The parentheses tell Perl to keep track of what that part of the regular expression matched. The variable \1 refers to this information. So the output string is "whatever you found before the space" plus a newline.

To do this without regular expressions, use multiple substitute statements:

    s/\. /\.\n/g; 
    s/! /!\n/g; 
    s/\? /\?\n/g; 

Back to labs/answer key page

-----

Emily M. Bender
Last modified: Fri Dec 8 11:59:13 2000