[tpm] New Regex question
Tom Legrady
legrady at gmail.com
Sun Oct 28 11:44:15 PDT 2012
On Sun, Oct 28, 2012 at 2:23 PM, Chris Jones <cj at enersave.ca> wrote:
> At 07:22 PM 27/10/2012, Uri Guttman wrote:
>
>> On 10/27/2012 06:57 PM, Rob Janes wrote:
>>
>>> $1 doesn't work.
>>>
>>> \1 is the characters matched while $1 is the pattern matched.
>>>
>>> It only matters if u want to match pure string.
>>>
>>
>> that is wrong. both are only references to a grab which is always a
>> string. you can use \1 in the regex to match what you have previously
>> matched and grabbed. $1 is used in the replacement or after the regex to
>> get what was grabbed. neither contains a regex.
>>
>> uri
>>
>
> Chapman states that \1 is used with in the pattern and $1 is used
> everywhere else. He offers this example:
>
> while(<>)
> {
> s/(\w+ and \1/$1 twice/;
> s/(w+) and (\w+)/$2 and $1/;
> }
>
> This example reverses pairs of different words, 'Marks and Spencer' into
> 'Spencer and Marks', and collapses identical pairs, taking 'Sattchi and
> Sattchi' into 'Sattchi twice'
>
> So, my example should be $exp =~ s/(\s|\r\n)\1+/$1/g;
>
You don't need \r\n ... \n is sufficient:
"\n matches a logical newline. Perl converts between \n and your
OS's native newline character when reading from or writing to text
files."
'Character Escapes'
in http://perldoc.perl.org/perlrebackslash.html
Also, do you want to wind up with either a space or a newline,
accidentally, depending on which one came first?
Consider "line 1 \n \nline 3" collapsing into "line1 line 3"
compared to "line 1\n \nline 3" collapsing into "line1\nline 3"
just because of a trailing space.
Tom
> >>
> Christopher Jones, P.Eng.
> Suite 1801, 1 Yonge Street
> Toronto, ON M5E1W7
> Tel. 416-203-7465
> Fax. 416-946-1005
> email cj at enersave.ca
>
> ______________________________**_________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/**listinfo/toronto-pm<http://mail.pm.org/mailman/listinfo/toronto-pm>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20121028/7f842a9d/attachment-0001.html>
More information about the toronto-pm
mailing list