in reply to Matching n characters with m//g

G'day medium.dave,

Welcome to the Monastery.

The braces are special in regexes. .{32760} matches 32,760 characters; it performs no match on the string containing 32760. See perlre: Quantifiers for more complete details.

What you need to do is escape the braces. Something like this:

$ perl -wE 'my $x = q[X{123}X]; say $x; $x =~ /([{]123[}])/; say $1' X{123}X {123}

Although you haven't shown the complete context of your code, the caret (^) in your regex looks dubious. Perhaps take a step back and read "perlretut - Perl regular expressions tutorial".

— Ken

Replies are listed 'Best First'.
Re^2: Matching n characters with m//g
by medium.dave (Novice) on Feb 22, 2016 at 04:25 UTC
    Thanks Ken, I am indeed attempting to extract than many characters, so the utilisation of the curly brackets in this instance is correct. Thanks for following up though, appreciated!

      OK, it looks like I confused "{32760}" and "{234565}"; however, I don't see "{234565}" anywhere in your code, although you state:

      "... when I come to the part "RFC822.TEXT {234565}" I now need to read exactly 234565 from the string."

      The string data you posted is multi-line (i.e. it contains newlines) so you'll need the 's' modifier if you want '.' to match newlines as well as other characters (see perlre: Modifiers). Here's a rough example of what I think you're looking for:

      $ perl -wE 'my $x = qq{abcd\nefgh\nijkl}; say ">$x<"; say ">$1<" while + $x =~ /(.{3})/gs' >abcd efgh ijkl< >abc< >d e< >fgh< > ij<

      [Note how I've wrapped each string in angle brackets, so that you can see not just the start and end of each string, but also the placement of newlines within them.]

      The caret (in your original code) still looks dubious. If it isn't, and you want to match the start of lines (in a multi-line string), you'll need the 'm' modifier (see perlre: Modifiers). Also, if you want to match the start of the entire string, use '\A' (see perlre: Assertions).

      — Ken