[tpm] New Regex question
janes.rob at gmail.com
Sun Oct 28 12:28:15 PDT 2012
[robj at rj-ul80vt ~]$ perl -e '$x="line 1 \n\nline 2"; $x =~ s/(\s)\1+/$1/g;
[robj at rj-ul80vt ~]$ perl -e '$x="line 1 \n\n line 2"; $x =~ s/(\s)\s+/$1/g;
line 1 line 2
this ones wrong.
On Sun, Oct 28, 2012 at 2:44 PM, Tom Legrady <legrady at gmail.com> wrote:
> 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.
>> Chapman states that \1 is used with in the pattern and $1 is used
>> everywhere else. He offers this example:
>> 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
> '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.
>> 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
> toronto-pm mailing list
> toronto-pm at pm.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the toronto-pm