PCRE: Use backreference in pattern repetition bracket

PCRE: Use backreference in pattern repetition bracket

am 25.08.2011 10:42:20 von Honza Mach

--=-V4JZtDxx7p7iE1Oc+jBw
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi everybody,

I was wondering, if it is possible to use backreferences in the pattern
repetition bracket operator.

Consider the following string:

my $string =3D "5 abcdefghijklmn";

The number five at the beginning of the string means, that I want to
extract first five characters from the latter part of the string. I
tried the following code, but it doesn`t work:

$string =3D~ s/(\d+)\s+(.{\g1})//;
print "extracted: $1 $2\n";

The desired output would be:

extracted: 5 abcde

It seems, that it is not possible to use backreferences within the
bracket operator (or am I doing something wrong?).

Is there other solution to my problem.

Than you in advance for your time

Regards

Honza Mach
Cesnet

--=-V4JZtDxx7p7iE1Oc+jBw
Content-Type: application/x-pkcs7-signature; name="smime.p7s"
Content-Disposition: attachment; filename="smime.p7s"
Content-Transfer-Encoding: base64

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEH AQAAoIIW4TCCBGYw
ggNOoAMCAQICEFEmCpMc4n+cw6VfeeByroIwDQYJKoZIhvcNAQEFBQAwgZMx CzAJBgNVBAYTAlVT
MQswCQYDVQQIEwJVVDEXMBUGA1UEBxMOU2FsdCBMYWtlIENpdHkxHjAcBgNV BAoTFVRoZSBVU0VS
VFJVU1QgTmV0d29yazEhMB8GA1UECxMYaHR0cDovL3d3dy51c2VydHJ1c3Qu Y29tMRswGQYDVQQD
ExJVVE4gLSBEQVRBQ29ycCBTR0MwHhcNMDUwNjA3MDgwOTEwWhcNMTkwNjI0 MTkwNjMwWjBvMQsw
CQYDVQQGEwJTRTEUMBIGA1UEChMLQWRkVHJ1c3QgQUIxJjAkBgNVBAsTHUFk ZFRydXN0IEV4dGVy
bmFsIFRUUCBOZXR3b3JrMSIwIAYDVQQDExlBZGRUcnVzdCBFeHRlcm5hbCBD QSBSb290MIIBIjAN
BgkqhkiG9w0BAQEFAA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TAk5MMQ8wDQYDVQQKEwZURVJFTkExGzAZBgNVBAMTElRFUkVOQSBQ ZXJzb25hbCBDQQIR
AK8Bj1PsBLthMCqZ0zq5h2owCQYFKw4DAhoFAKCCASEwGAYJKoZIhvcNAQkD MQsGCSqGSIb3DQEH
ATAcBgkqhkiG9w0BCQUxDxcNMTEwODI1MDg0MjE3WjAjBgkqhkiG9w0BCQQx FgQUs5zsYoZ1yLGd
HOKK38/juciCv50wXwYJKwYBBAGCNxAEMVIwUDA7MQswCQYDVQQGEwJOTDEP MA0GA1UEChMGVEVS
RU5BMRswGQYDVQQDExJURVJFTkEgUGVyc29uYWwgQ0ECEQCvAY9T7AS7YTAq mdM6uYdqMGEGCyqG
SIb3DQEJEAILMVKgUDA7MQswCQYDVQQGEwJOTDEPMA0GA1UEChMGVEVSRU5B MRswGQYDVQQDExJU
RVJFTkEgUGVyc29uYWwgQ0ECEQCvAY9T7AS7YTAqmdM6uYdqMA0GCSqGSIb3 DQEBAQUABIIBALuC
OHpS8v/tuf+fZMnXJkdKqy6+6fpHJcuwl4OxjebpezyHeRhUtFo0FxXbbc0/ ez8aWdtEfPZ/xrKS
7H3w49KUE9CaQLuyDmW4llzVc+TjAzq7BC4Bdf3vgvS630l/TGQ3hy6tl/Iz sd+Y3BM7eft9OSpw
6tzhB+PAW2xnMZ5NRiXpyWZRD7jslfqAQMvNIT65U4RjuM0hauOQD2wMSLaa h+9+qI2gG+1782wU
V/Q4OGX73rbLZtypMCYHY/QEMMVbHqRWCvq1RZLqwfzP7O1/KrXkckSrHKD1 qbunKATz0FpCP8aD
Q8i0sRTJacRk0nQ24tCiYN8Bh81DlJxq4JMAAAAAAAA=


--=-V4JZtDxx7p7iE1Oc+jBw--

Re: PCRE: Use backreference in pattern repetition bracket

am 25.08.2011 10:56:45 von Shlomi Fish

ׁHi Honza,

On Thu, 25 Aug 2011 10:42:20 +0200
Honza Mach wrote:

> Hi everybody,
>=20
> I was wondering, if it is possible to use backreferences in the pattern
> repetition bracket operator.
>=20
> Consider the following string:
>=20
> my $string =3D "5 abcdefghijklmn";
>=20
> The number five at the beginning of the string means, that I want to
> extract first five characters from the latter part of the string. I
> tried the following code, but it doesn`t work:
>=20
> $string =3D~ s/(\d+)\s+(.{\g1})//;
> print "extracted: $1 $2\n";
>=20
> The desired output would be:
>=20
> extracted: 5 abcde
>=20
> It seems, that it is not possible to use backreferences within the
> bracket operator (or am I doing something wrong?).
>=20

I don't think it's possible, because the perl regex engine is not that smar=
t.

> Is there other solution to my problem.
>=20

You can use incremental parsing using \G and /g. See:

http://perl-begin.org/uses/text-parsing/

Regards,

Shlomi Fish


--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Freecell Solver - http://fc-solve.berlios.de/

Knuth is not God! Typing â€=9CGodâ€=9D into Google and pressing =E2=
€œIâ€=99m Feeling Luckyâ€=9D
will not lead you to his homepage.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: PCRE: Use backreference in pattern repetition bracket

am 25.08.2011 11:13:37 von Honza Mach

--=-hvjggRTzZhfaoKmTiRl9
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi Schlomi,

thank you so much for such a quick response.

On Thu, 2011-08-25 at 11:56 +0300, Shlomi Fish wrote:
> ׁHi Honza,
>=20
> On Thu, 25 Aug 2011 10:42:20 +0200
> Honza Mach wrote:
>=20
> > Hi everybody,
> >=20
> > I was wondering, if it is possible to use backreferences in the pattern
> > repetition bracket operator.
> >=20
> > Consider the following string:
> >=20
> > my $string =3D "5 abcdefghijklmn";
> >=20
> > The number five at the beginning of the string means, that I want to
> > extract first five characters from the latter part of the string. I
> > tried the following code, but it doesn`t work:
> >=20
> > $string =3D~ s/(\d+)\s+(.{\g1})//;
> > print "extracted: $1 $2\n";
> >=20
> > The desired output would be:
> >=20
> > extracted: 5 abcde
> >=20
> > It seems, that it is not possible to use backreferences within the
> > bracket operator (or am I doing something wrong?).
> >=20
>=20
> I don't think it's possible, because the perl regex engine is not that sm=
art.

That`s a pity, but I will walk around it somehow.

> > Is there other solution to my problem.
> >=20
>=20
> You can use incremental parsing using \G and /g. See:
>=20
> http://perl-begin.org/uses/text-parsing/

Thank you for the tip, I will check it out.

> Regards,
>=20
> Shlomi Fish
>=20

Regards

Honza Mach

> --=20
> ------------------------------------------------------------ -----
> Shlomi Fish http://www.shlomifish.org/
> Freecell Solver - http://fc-solve.berlios.de/
>=20
> Knuth is not God! Typing â€=9CGodâ€=9D into Google and pressing =
â€=9CIâ€=99m Feeling Luckyâ€=9D
> will not lead you to his homepage.
>=20
> Please reply to list if it's a mailing list post - http://shlom.in/reply =
..
>=20


--=-hvjggRTzZhfaoKmTiRl9
Content-Type: application/x-pkcs7-signature; name="smime.p7s"
Content-Disposition: attachment; filename="smime.p7s"
Content-Transfer-Encoding: base64

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEH AQAAoIIW4TCCBGYw
ggNOoAMCAQICEFEmCpMc4n+cw6VfeeByroIwDQYJKoZIhvcNAQEFBQAwgZMx CzAJBgNVBAYTAlVT
MQswCQYDVQQIEwJVVDEXMBUGA1UEBxMOU2FsdCBMYWtlIENpdHkxHjAcBgNV BAoTFVRoZSBVU0VS
VFJVU1QgTmV0d29yazEhMB8GA1UECxMYaHR0cDovL3d3dy51c2VydHJ1c3Qu Y29tMRswGQYDVQQD
ExJVVE4gLSBEQVRBQ29ycCBTR0MwHhcNMDUwNjA3MDgwOTEwWhcNMTkwNjI0 MTkwNjMwWjBvMQsw
CQYDVQQGEwJTRTEUMBIGA1UEChMLQWRkVHJ1c3QgQUIxJjAkBgNVBAsTHUFk ZFRydXN0IEV4dGVy
bmFsIFRUUCBOZXR3b3JrMSIwIAYDVQQDExlBZGRUcnVzdCBFeHRlcm5hbCBD QSBSb290MIIBIjAN
BgkqhkiG9w0B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gNVBAYTAk5MMQ8wDQYDVQQKEwZURVJFTkExGzAZBgNVBAMTElRFUkVOQSBQ ZXJzb25hbCBDQQIR
AK8Bj1PsBLthMCqZ0zq5h2owCQYFKw4DAhoFAKCCASEwGAYJKoZIhvcNAQkD MQsGCSqGSIb3DQEH
ATAcBgkqhkiG9w0BCQUxDxcNMTEwODI1MDkxMzM3WjAjBgkqhkiG9w0BCQQx FgQU4M46VoB8CCAv
w/NBep5mo7+Bk/UwXwYJKwYBBAGCNxAEMVIwUDA7MQswCQYDVQQGEwJOTDEP MA0GA1UEChMGVEVS
RU5BMRswGQYDVQQDExJURVJFTkEgUGVyc29uYWwgQ0ECEQCvAY9T7AS7YTAq mdM6uYdqMGEGCyqG
SIb3DQEJEAILMVKgUDA7MQswCQYDVQQGEwJOTDEPMA0GA1UEChMGVEVSRU5B MRswGQYDVQQDExJU
RVJFTkEgUGVyc29uYWwgQ0ECEQCvAY9T7AS7YTAqmdM6uYdqMA0GCSqGSIb3 DQEBAQUABIIBAE+4
Zt0Y7cwuGFsyIWIOm1Jzc8UowN1oYSQ4oywxcGjdyWZxIWjmkfeq1eJ5WR7T bv1ml+IwCkh2DmGj
YTqQ4r2qDyT1EK8Wcwl7eOR6FGU22tga/hPBP0fcbInhAo/V5GUOts0KHhRb 2A0xiUuGQY/YPpj4
yUei7sBOEPpRRMUGk5ZAjnHcL++0Nlel1kM1xrRYM8tOV7djSDopcGJ6gWvQ d5SJYk2nE6cVRANj
BJXZ+bWCLPQothTemNZ5xXb1XjyKqr2z+ao/nor7glZqaj5v3Y1YC62z+3Vi etalTASnqsLBkFod
8tN7394d6AzuiTxNkwMAhJf1AMvqV8dad/wAAAAAAAA=


--=-hvjggRTzZhfaoKmTiRl9--

Re: PCRE: Use backreference in pattern repetition bracket

am 25.08.2011 11:35:10 von Paul Johnson

> > On Thu, 25 Aug 2011 10:42:20 +0200
> > Honza Mach wrote:
> >
> > > Hi everybody,
> > >
> > > I was wondering, if it is possible to use backreferences in the pattern
> > > repetition bracket operator.
> > >
> > > Consider the following string:
> > >
> > > my $string = "5 abcdefghijklmn";
> > >
> > > The number five at the beginning of the string means, that I want to
> > > extract first five characters from the latter part of the string. I
> > > tried the following code, but it doesn`t work:
> > >
> > > $string =~ s/(\d+)\s+(.{\g1})//;
> > > print "extracted: $1 $2\n";
> > >
> > > The desired output would be:
> > >
> > > extracted: 5 abcde

Something like this will work:

say "extracted: ", /^(\d+\s+.{$len})/ if ($len) = /^(\d+)/

Sometimes two passes are are better than one.

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: PCRE: Use backreference in pattern repetition bracket

am 25.08.2011 12:02:37 von Honza Mach

--=-Gw8KP/zW/WZf2sctlsAx
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi,

On Thu, 2011-08-25 at 11:35 +0200, Paul Johnson wrote:
> > > On Thu, 25 Aug 2011 10:42:20 +0200
> > > Honza Mach wrote:
> > >=20
> > > > Hi everybody,
> > > >=20
> > > > I was wondering, if it is possible to use backreferences in the pat=
tern
> > > > repetition bracket operator.
> > > >=20
> > > > Consider the following string:
> > > >=20
> > > > my $string =3D "5 abcdefghijklmn";
> > > >=20
> > > > The number five at the beginning of the string means, that I want t=
o
> > > > extract first five characters from the latter part of the string. I
> > > > tried the following code, but it doesn`t work:
> > > >=20
> > > > $string =3D~ s/(\d+)\s+(.{\g1})//;
> > > > print "extracted: $1 $2\n";
> > > >=20
> > > > The desired output would be:
> > > >=20
> > > > extracted: 5 abcde
>=20
> Something like this will work:
>=20
> say "extracted: ", /^(\d+\s+.{$len})/ if ($len) =3D /^(\d+)/
> Sometimes two passes are are better than one.
>=20

Thank you for your advice, however I wanted to do it in one pass because
of the following reasons:

1. I wanted to compile the pattern because of the performance
(I guess it is not possible to compile the pattern, when there are =20
some substitutions of the variables from outside of the RE engine,=20
or am I wrong?)
2. Perhaps I should have mentioned, that the application is parsing the=20
continuos string stream coming from a socket, there are many of=20
these "commands" coming one after each other and there will not=20
always be the complete command present in the buffer, next chunk=20
may come later. I was actually trying to use it this way:

my $string =3D "5 abcde 2 fg 3 hij 6 klm";
while ($string =3D~ s/\s*(\d+)\s+(.{\1})//)
{
print "extracted: $1 $2\n";
}=20

This way the while loop elegantly fails, when the pattern does not=20
match and when the next data arrives, the check is made again.

I was just trying to find the most elegant solution, but if it is not
possible, I will create some work around, that is not the problem.

But thank you all for your time and advice.

Regards

Honza Mach


--=-Gw8KP/zW/WZf2sctlsAx
Content-Type: application/x-pkcs7-signature; name="smime.p7s"
Content-Disposition: attachment; filename="smime.p7s"
Content-Transfer-Encoding: base64

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEH AQAAoIIW4TCCBGYw
ggNOoAMCAQICEFEmCpMc4n+cw6VfeeByroIwDQYJKoZIhvcNAQEFBQAwgZMx CzAJBgNVBAYTAlVT
MQswCQYDVQQIEwJVVDEXMBUGA1UEBxMOU2FsdCBMYWtlIENpdHkxHjAcBgNV BAoTFVRoZSBVU0VS
VFJVU1QgTmV0d29yazEhMB8GA1UECxMYaHR0cDovL3d3dy51c2VydHJ1c3Qu Y29tMRswGQYDVQQD
ExJVVE4gLSBEQVRBQ29ycCBTR0MwHhcNMDUwNjA3MDgwOTEwWhcNMTkwNjI0 MTkwNjMwWjBvMQsw
CQYDVQQGEwJTRTEUMBIGA1UEChMLQWRkVHJ1c3QgQUIxJjAkBgNVBAsTHUFk ZFRydXN0IEV4dGVy
bmFsIFRUUCBOZXR3b3JrMSIwIAYDVQQDExlBZGRUcnVzdCBFeHRlcm5hbCBD QSBSb290MIIBIjAN
BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt/caM+byAAQtOeBOW+0fvGwP zbX6I7bO3psRM5ek
KUx9k5+9SryT7QMa44/P5W1QWtaXKZRagLBJetsulf24yr83OC0ePp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FAKCCASEwGAYJKoZIhvcNAQkD MQsGCSqGSIb3DQEH
ATAcBgkqhkiG9w0BCQUxDxcNMTEwODI1MTAwMjM3WjAjBgkqhkiG9w0BCQQx FgQU6KhmWaD3MqAI
UTuvV2OlbW6a93IwXwYJKwYBBAGCNxAEMVIwUDA7MQswCQYDVQQGEwJOTDEP MA0GA1UEChMGVEVS
RU5BMRswGQYDVQQDExJURVJFTkEgUGVyc29uYWwgQ0ECEQCvAY9T7AS7YTAq mdM6uYdqMGEGCyqG
SIb3DQEJEAILMVKgUDA7MQswCQYDVQQGEwJOTDEPMA0GA1UEChMGVEVSRU5B MRswGQYDVQQDExJU
RVJFTkEgUGVyc29uYWwgQ0ECEQCvAY9T7AS7YTAqmdM6uYdqMA0GCSqGSIb3 DQEBAQUABIIBAMjg
eZS2oJK+eIE82LoFA3j6eWLvzEnOPNCVIE/JI3rTysc3+qD+GiwPfClZ2r+z cUd4TApJVDxhAR1E
abfkiWffhFH8YbbwhG3DJ5aAVd+aXJsAgRgv3My3LcO4jvBautt+JmZ3Ejp3 GXbwaosjWySuWy1A
ifh1rmYN5aHtI0hTzWJKjckerAArfoQ8Q2u4ZKuNDc8uwytMM+NQppFIJVR6 9evi3EgBOXb+7L4O
trzN1jqak2FT9+lNAUT85zDn3ia6JjJYlWwruQbX5XREcz3OZYsGJVxVaJSa DQwBDPM7/2oQvCNy
/Fwa7T0bWZwC10vlReyoEQ3YVwhAw49bLBAAAAAAAAA=


--=-Gw8KP/zW/WZf2sctlsAx--

Re: PCRE: Use backreference in pattern repetition bracket

am 25.08.2011 12:54:00 von Paul Johnson

On Thu, Aug 25, 2011 at 12:02:37PM +0200, Honza Mach wrote:
> Hi,
>
> On Thu, 2011-08-25 at 11:35 +0200, Paul Johnson wrote:
> >
> > say "extracted: ", /^(\d+\s+.{$len})/ if ($len) = /^(\d+)/
> > Sometimes two passes are are better than one.
> >
>
> Thank you for your advice, however I wanted to do it in one pass because
> of the following reasons:
>
> 1. I wanted to compile the pattern because of the performance

Ah. Then you have already benchmarked a solution, found it to be too
slow, profiled your code and discovered that this section of the code
was the bottleneck in such a way that compiling the regualar expression
would solve the problem?

Why on earth are you worried about performance? Especially when your
data is coming from a socket. I suggest not worrying about performance
until you need to.

Please don't think I'm geting at you personally here. Perhaps you have
already done this. But in general there is far too much talk about
speed on this list. Make your code correct. Make it nice. Until that
point performance cannot be a concern.

> (I guess it is not possible to compile the pattern, when there are
> some substitutions of the variables from outside of the RE engine,
> or am I wrong?)

See below.

> 2. Perhaps I should have mentioned, that the application is parsing the
> continuos string stream coming from a socket, there are many of
> these "commands" coming one after each other and there will not
> always be the complete command present in the buffer, next chunk
> may come later. I was actually trying to use it this way:
>
> my $string = "5 abcde 2 fg 3 hij 6 klm";
> while ($string =~ s/\s*(\d+)\s+(.{\1})//)
> {
> print "extracted: $1 $2\n";
> }
>
> This way the while loop elegantly fails, when the pattern does not
> match and when the next data arrives, the check is made again.

OK. So try this:

say "extracted: $1 $2" while s/\s*(\d+)(?{$len = $^N})\s+((??{"." x $len}))//

That uses features marked as experimental. It would probably be a good
idea to read perlre to understand them. You can even compile that if
you want to but I'm not sure what difference, if any, you will notice in
doing so.

> I was just trying to find the most elegant solution, but if it is not
> possible, I will create some work around, that is not the problem.

Some might class this as an elegant solution. Others might take the
opposite position.

Good luck,

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: PCRE: Use backreference in pattern repetition bracket

am 25.08.2011 13:05:54 von Honza Mach

--=-fUGoA3Hbr88fcH02+5Ln
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi,

On Thu, 2011-08-25 at 12:54 +0200, Paul Johnson wrote:
> On Thu, Aug 25, 2011 at 12:02:37PM +0200, Honza Mach wrote:
> > Hi,
> >=20
> > On Thu, 2011-08-25 at 11:35 +0200, Paul Johnson wrote:
> > >=20
> > > say "extracted: ", /^(\d+\s+.{$len})/ if ($len) =3D /^(\d+)/
> > > Sometimes two passes are are better than one.
> > >=20
> >=20
> > Thank you for your advice, however I wanted to do it in one pass becaus=
e
> > of the following reasons:
> >=20
> > 1. I wanted to compile the pattern because of the performance
>=20
> Ah. Then you have already benchmarked a solution, found it to be too
> slow, profiled your code and discovered that this section of the code
> was the bottleneck in such a way that compiling the regualar expression
> would solve the problem?
>=20
> Why on earth are you worried about performance? Especially when your
> data is coming from a socket. I suggest not worrying about performance
> until you need to.
>=20
> Please don't think I'm geting at you personally here. Perhaps you have
> already done this. But in general there is far too much talk about
> speed on this list. Make your code correct. Make it nice. Until that
> point performance cannot be a concern.

Point taken, you are right. I haven`t done any benchmarking so far, but
I wanted it to be as effective as possible, because the system I am
bulding needs to be capable of handling as many events per second as
possible and it seems I got caught in the over-optimizing trap every
developer should avoid ;)

> > (I guess it is not possible to compile the pattern, when there are =
=20
> > some substitutions of the variables from outside of the RE engine,=
=20
> > or am I wrong?)
>=20
> See below.
>=20
> > 2. Perhaps I should have mentioned, that the application is parsing the=
=20
> > continuos string stream coming from a socket, there are many of=20
> > these "commands" coming one after each other and there will not=20
> > always be the complete command present in the buffer, next chunk=20
> > may come later. I was actually trying to use it this way:
> >=20
> > my $string =3D "5 abcde 2 fg 3 hij 6 klm";
> > while ($string =3D~ s/\s*(\d+)\s+(.{\1})//)
> > {
> > print "extracted: $1 $2\n";
> > }=20
> >=20
> > This way the while loop elegantly fails, when the pattern does not=
=20
> > match and when the next data arrives, the check is made again.
>=20
> OK. So try this:
>=20
> say "extracted: $1 $2" while s/\s*(\d+)(?{$len =3D $^N})\s+((??{"." x $=
len}))//
>=20
> That uses features marked as experimental. It would probably be a good
> idea to read perlre to understand them. You can even compile that if
> you want to but I'm not sure what difference, if any, you will notice in
> doing so.

Thank you for the idea, I`ll try that.

> > I was just trying to find the most elegant solution, but if it is not
> > possible, I will create some work around, that is not the problem.
>=20
> Some might class this as an elegant solution. Others might take the
> opposite position.
>=20
> Good luck,


Thanks for the advices

Honza Mach


--=-fUGoA3Hbr88fcH02+5Ln
Content-Type: application/x-pkcs7-signature; name="smime.p7s"
Content-Disposition: attachment; filename="smime.p7s"
Content-Transfer-Encoding: base64

MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEH AQAAoIIW4TCCBGYw
ggNOoAMCAQICEFEmCpMc4n+cw6VfeeByroIwDQYJKoZIhvcNAQEFBQAwgZMx CzAJBgNVBAYTAlVT
MQswCQYDVQQIEwJVVDEXMBUGA1UEBxMOU2FsdCBMYWtlIENpdHkxHjAcBgNV BAoTFVRoZSBVU0VS
VFJVU1QgTmV0d29yazEhMB8GA1UECxMYaHR0cDovL3d3dy51c2VydHJ1c3Qu Y29tMRswGQYDVQQD
ExJVVE4gLSBEQVRBQ29ycCBTR0MwHhcNMDUwNjA3MDgwOTEwWhcNMTkwNjI0 MTkwNjMwWjBvMQsw
CQYDVQQGEwJTRTEUMBIGA1UEChMLQWRkVHJ1c3QgQUIxJjAkBgNVBAsTHUFk ZFRydXN0IEV4dGVy
bmFsIFRUUCBOZXR3b3JrMSIwIAYDVQQDExlBZGRUcnVzdCBFeHRlcm5hbCBD QSBSb290MIIBIjAN
BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt/caM+byAAQtOeBOW+0fvGwP zbX6I7bO3psRM5ek
KUx9k5+9SryT7QMa44/P5W1QWtaXKZRagLBJetsulf24yr83OC0ePpFBrXBW x/BPP+gynnTKyJBU
6cZfD3idmkA8Dqxhql4Uj56HoWpQ3NeaTq8Fs6ZxlJxxs1BgCscTnTgHhgKo 6ahpJhiQq0ywTyOr
Ok+E2N/On+Fpb7vXQtdrROTHre5tQV9yWnEIN7N5ZaRZoJQ39wAvDcKSctrQ OHLbFKhFxF0qfbe0
1sTurM0TRLfJK91DACX6YblpalgjEbenM49WdVn1zSnXRrcKK2W200JvFbK4 e/vv6V1T1TRaJwID
AQABo4HYMIHVMB8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qG
SIb3DQEJEAILMVKgUDA7MQswCQYDVQQGEwJOTDEPMA0GA1UEChMGVEVSRU5B MRswGQYDVQQDExJU
RVJFTkEgUGVyc29uYWwgQ0ECEQCvAY9T7AS7YTAqmdM6uYdqMA0GCSqGSIb3 DQEBAQUABIIBAKBM
Ww/E0D5bYRQGKM308FuJsqxczd+HTcaEsERx+8wQxmzQjzLDrRnl45jH5UzF LepzrvjlRZiMbVw0
LJHBVOeFPaJUfWjZl2E3ggh3TRR3iddsS65oqKP8RN+4UOC5wLMp9ACFyt++ deyK2wCpuOeZNe/8
pnEmP++Xd0H0+y2ftpWro/bRpMFq11I5WvKkCjpCJWHcDloqFpbSrBTDM17q lWn5TB0mV0GrWXK4
52HxFcLqnVnYNJa0S/z4QHP2YdRzNmVA+tsWZEW3Le52tnX4BJGTn6eI05gI D2PPWA/HwAYK99pj
LX1HCTitKgVbC+jOpL4jK9q6/J42CC20fgMAAAAAAAA=


--=-fUGoA3Hbr88fcH02+5Ln--

Re: PCRE: Use backreference in pattern repetition bracket

am 25.08.2011 16:01:00 von jwkrahn

Honza Mach wrote:
> Hi everybody,

Hello,

> I was wondering, if it is possible to use backreferences in the pattern
> repetition bracket operator.
>
> Consider the following string:
>
> my $string = "5 abcdefghijklmn";
>
> The number five at the beginning of the string means, that I want to
> extract first five characters from the latter part of the string. I
> tried the following code, but it doesn`t work:
>
> $string =~ s/(\d+)\s+(.{\g1})//;
> print "extracted: $1 $2\n";
>
> The desired output would be:
>
> extracted: 5 abcde
>
> It seems, that it is not possible to use backreferences within the
> bracket operator (or am I doing something wrong?).
>
> Is there other solution to my problem.

If it always involves a single digit followed by a single space you
could use unpack()

$ perl -le'
my $string = "5 abcdefghijklmn";
my ( $num, $extract ) = unpack "A2 X2 A2/a*", $string;
print "$num $extract";
'
5 abcde




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: PCRE: Use backreference in pattern repetition bracket

am 25.08.2011 19:12:57 von merlyn

>>>>> "Honza" == Honza Mach writes:

Honza> Hi everybody,

In the subject, you mention "PCRE". Are you actually using PCRE, and
not Perl? If so, this mailing list isn't for you, because PCRE is
misnamed: it isn't Perl, and it's not even Perl Compatible. This mailing
list will likely give you Perl answers that have nothing to do with
PCRE.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: PCRE: Use backreference in pattern repetition bracket

am 25.08.2011 21:20:06 von derykus

On Aug 25, 7:01=A0am, jwkr...@shaw.ca ("John W. Krahn") wrote:
> Honza Mach wrote:
> > Hi everybody,
>
> Hello,
>
>
>
>
>
>
>
>
>
> > I was wondering, if it is possible to use backreferences in the pattern
> > repetition bracket operator.
>
> > Consider the following string:
>
> > my $string =3D "5 abcdefghijklmn";
>
> > The number five at the beginning of the string means, that I want to
> > extract first five characters from the latter part of the string. I
> > tried the following code, but it doesn`t work:
>
> > $string =3D~ s/(\d+)\s+(.{\g1})//;
> > print "extracted: $1 $2\n";
>
> > The desired output would be:
>
> > extracted: 5 abcde
>
> > It seems, that it is not possible to use backreferences within the
> > bracket operator (or am I doing something wrong?).
>
> > Is there other solution to my problem.
>
> If it always involves a single digit followed by a single space you
> could use unpack()
>
> $ perl -le'
> my $string =3D "5 abcdefghijklmn";
> my ( $num, $extract ) =3D unpack "A2 X2 A2/a*", $string;
> print "$num $extract";
> '
> 5 abcde
>

Or, eliminating the single digit requirement
with split/substr:

( $num, $ss ) =3D split " ", $string;
print "$num ", substr $ss, 0 , $num;

--
Charles DeRykus



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/