Search and replace w/ ignoring arbitrary characters

Search and replace w/ ignoring arbitrary characters

am 08.08.2011 16:31:14 von pottwal1

dear all,

i want to make some search and replace within a string where I can
define a set of characters, especially parenthesis, brackets etc.,
which are to be ignored.

For example, I have the following string:

sdjfh sdf sjkdfh sdkjfh sdjkf f[o]o(bar) hsdkjfh sdkljfhs sjfh sdkj
sdjkfh sdjfh ska

A search and replace for 'foobar' should result in the following
string:

sdjfh sdf sjkdfh sdkjfh sdjkf ##f[o]o(bar)## hsdkjfh sdkljfhs sjfh
sdkj sdjkfh sdjfh ska

I tried doing this with Unicode::Collate but with this module the
search and replace is very slow and lacks some flexibility.

Is it possible to make this kind of search and replace with a regular
expression, or does somebody know of an existing perl module which can
do this?

tia,
Frank


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

Re: Search and replace w/ ignoring arbitrary characters

am 09.08.2011 02:34:38 von jwkrahn

Frank Müller wrote:
> dear all,

Hello,

> i want to make some search and replace within a string where I can
> define a set of characters, especially parenthesis, brackets etc.,
> which are to be ignored.
>
> For example, I have the following string:
>
> sdjfh sdf sjkdfh sdkjfh sdjkf f[o]o(bar) hsdkjfh sdkljfhs sjfh sdkj
> sdjkfh sdjfh ska
>
> A search and replace for 'foobar' should result in the following
> string:
>
> sdjfh sdf sjkdfh sdkjfh sdjkf ##f[o]o(bar)## hsdkjfh sdkljfhs sjfh
> sdkj sdjkfh sdjfh ska

$ perl -le'
$_ =3D "sdjfh sdf sjkdfh sdkjfh sdjkf f[o]o(bar) hsdkjfh sdkljfhs sjfh=20
sdkj sdjkfh sdjfh ska";
print;
s{ ( \S+ ) }{ ( $x =3D $1 ) =3D~ tr/a-zA-Z//cd; "foobar" eq lc $x ? "##$1=
##"=20
: $1 }xeg;
print;
'
sdjfh sdf sjkdfh sdkjfh sdjkf f[o]o(bar) hsdkjfh sdkljfhs sjfh sdkj=20
sdjkfh sdjfh ska
sdjfh sdf sjkdfh sdkjfh sdjkf ##f[o]o(bar)## hsdkjfh sdkljfhs sjfh sdkj=20
sdjkfh sdjfh ska




John
--=20
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

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

Re: Search and replace w/ ignoring arbitrary characters

am 11.08.2011 08:56:50 von pottwal1

hi,
thank you for your solution which works perfect for the data i have
given.
the trouble is: my data looks a little more complex as I have lots of
accented characters so with your code I need to specify each of those
characters in the tr/// part. I reckon the other way around would be
more usefull: speceifying all characters that can be irgnored for
matching. Is there any chance of doing this?
And a second small issue: is it possible to change the code so that
not only full words are marked by the double ##-characters but also
parts of it, e.g. searching for 'ooba' would result in
f[##o]o(ba##r) ?
again, thanks for any ideas and hints,
Frank


On 9 Aug., 02:34, jwkr...@shaw.ca ("John W. Krahn") wrote:
> Frank Müller wrote:
> > dear all,
>
> Hello,
>
> > i want to make some search and replace within a string where I can
> > define a set of characters, especially parenthesis, brackets etc.,
> > which are to be ignored.
>
> > For example, I have the following string:
>
> > sdjfh sdf sjkdfh sdkjfh sdjkf f[o]o(bar) hsdkjfh sdkljfhs =A0sjfh sdkj
> > sdjkfh sdjfh ska
>
> > A search and replace for 'foobar' should result in the following
> > string:
>
> > sdjfh sdf sjkdfh sdkjfh sdjkf ##f[o]o(bar)## hsdkjfh sdkljfhs =A0sjfh
> > sdkj sdjkfh sdjfh ska
>
> $ perl -le'
> $_ =3D "sdjfh sdf sjkdfh sdkjfh sdjkf f[o]o(bar) hsdkjfh sdkljfhs =A0sjfh
> sdkj sdjkfh sdjfh ska";
> print;
> s{ ( \S+ ) }{ ( $x =3D $1 ) =3D~ tr/a-zA-Z//cd; "foobar" eq lc $x ? "##$1=
##"
> : $1 }xeg;
> print;
> '
> sdjfh sdf sjkdfh sdkjfh sdjkf f[o]o(bar) hsdkjfh sdkljfhs =A0sjfh sdkj
> sdjkfh sdjfh ska
> sdjfh sdf sjkdfh sdkjfh sdjkf ##f[o]o(bar)## hsdkjfh sdkljfhs =A0sjfh sdk=
j
> sdjkfh sdjfh ska
>
> 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. =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- Albert Einstein


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