[Linux-disciples] A regex question

Adam Kessel linux-disciples@bostoncoop.net
Wed, 28 Jan 2004 22:35:20 -0500


--rS8CxjVDS/+yyDmU
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

You should look at perldoc perlre (the perl regexp manpage).

You need to use \1 rather than $1 if you want to match something from
earlier within the same regexp.  $1 only works in the replacement part of
a search/replace.

Also [^blah] doesn't mean "anything but blah," but rather, any character
but b, l, a, or h.  You can't use the [^abc] construct for an atom like
that. For that, you won't (?!blah).  See 'zero-width negative look-ahead
assertion' in perlre.

So, without actually testing it, I think your regexp needs to be
something more like:

/(.)(?!\1)\1\[$sourceKey\]/

but you'll have to try it.

On Wed, Jan 28, 2004 at 07:07:50PM -0500, Stephen R Laniel wrote:
> I'm looking to write a regex that says "Find me any character
> (which we'll remember as $1), followed by any number of other
> characters which can be anything except for $1, followed by $1
> again, followed by a literal bracket, a string from a given set
> of strings, and a closing bracket." The regex I've tried to use
> for this is
>=20
> (.)([^$1]*)$1\[$sourceKey\]
>=20
> I don't think Perl likes the [^$1] syntax very much; it's
> complaining whenever I run this script.
>=20
> I've also tried using minimal matching, viz.
>=20
> (.)(.*?)$1\[$sourceKey\]
>=20
> but for some reason the second occurrence of $1 seems to get
> caught inside of $2.
>=20
> Any idea what I'm doing wrong?
>=20
> --=20
> ``You will see light in the darkness
>   You will make some sense of this''
>  -The Police, "Secret Journey"
>=20
> _______________________________________________
> Linux-disciples mailing list
> Linux-disciples@bostoncoop.net
> http://bostoncoop.net/mailman/listinfo/linux-disciples

--=20
Adam Kessel
http://bostoncoop.net/adam

--rS8CxjVDS/+yyDmU
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAGH94dTf3ZklQ6qYRAtxwAJsFHgKo/P3H6P6hJ0OqTvOWoHt5rgCeMbzM
M0QMcjuIbeg3399lIKWtAG8=
=DD45
-----END PGP SIGNATURE-----

--rS8CxjVDS/+yyDmU--