how to use regexp to match symbols

--0-1914841569-1307966739=:16504
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Hi,=0AI have a list of mp3 files in my computer=A0and some of the file name=
s consists of=A0 a bracket like this "darling I love [you.mp3"=0AI wish to =
check them for duplicates using the script below, but theres error msg like=
this "Unmatched [ in regex; marked by <-- HERE in m/only one brace here[ <=
-- HERE anything.mp3/ at Untitled1 line 13."=0A=A0=0ASo how do I rewrite th=
e regexp.=0AThanks.=0A=A0=0A###### script ###=0A#!/usr/bin/perl=0Ause stric=
t;=0Ause warnings;=0Ause File::Find;=0A=A0=0Amy [at] datas =3D ("test.mp3" , "o=
nly one brace here[anything.mp3" , "whatever.mp3");=0A=0Awhile ( [at] datas){=0A=
=A0=A0=A0=A0=A0 my $ref =3D splice [at] datas,0,1;=0A=A0=A0=A0=A0=A0 foreach ( [at] =
datas){=0A=A0=A0=A0=A0=A0=A0=A0=A0=A0 if ($ref =3D~/$_/){=0A=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0 print "$ref is a duplicate\n";=0A=A0=A0=A0=A0=A0=A0=
=A0=A0=A0 }else{=0A=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 print "$ref is not =
a duplicate\n";=0A=A0=A0=A0=A0=A0=A0=A0=A0=A0 }=0A=A0=A0=A0=A0=A0 }=0A}
--0-1914841569-1307966739=:16504--
eventual [ Mo, 13 Juni 2011 14:05 ] [ ID #2060993 ]

Re: how to use regexp to match symbols

--0016e64ea22a1dba4104a596ddb5
Content-Type: text/plain; charset=UTF-8

On Mon, Jun 13, 2011 at 2:05 PM, eventual <eventualdeath [at] yahoo.com> wrote:

> Hi,
> I have a list of mp3 files in my computer and some of the file names
> consists of a bracket like this "darling I love [you.mp3"
> I wish to check them for duplicates using the script below, but theres
> error msg like this "Unmatched [ in regex; marked by <-- HERE in m/only one
> brace here[ <-- HERE anything.mp3/ at Untitled1 line 13."
>
> So how do I rewrite the regexp.
> Thanks.
>
> ###### script ###
> #!/usr/bin/perl
> use strict;
> use warnings;
> use File::Find;
>
> my [at] datas = ("test.mp3" , "only one brace here[anything.mp3" ,
> "whatever.mp3");
>
> while ( [at] datas){
> my $ref = splice [at] datas,0,1;
> foreach ( [at] datas){
> if ($ref =~/$_/){
> print "$ref is a duplicate\n";
> }else{
> print "$ref is not a duplicate\n";
> }
> }
> }


Escape the "special" character by using a \ so in your case you would
say: "only one brace here\[anything.mp3" which the regular expression engine
will translate to: "only one brace here[anything.mp3" instead of "only one
brace here<Open a caracter group>anything.mp3" which would mean you never
close the group and thus the regular expression is invalid and will throw an
error.

Regards,

Rob

--0016e64ea22a1dba4104a596ddb5--
Rob Coops [ Mo, 13 Juni 2011 14:14 ] [ ID #2060994 ]

Re: how to use regexp to match symbols

eventual wrote:
> Hi,

Hello,

> I have a list of mp3 files in my computer and some of the file names
> consists of a bracket like this "darling I love [you.mp3"
> I wish to check them for duplicates using the script below, but theres
> error msg like this "Unmatched [ in regex; marked by<-- HERE in m/only
> one brace here[<-- HERE anything.mp3/ at Untitled1 line 13."
>
> So how do I rewrite the regexp.
> Thanks.
>
> ###### script ###
> #!/usr/bin/perl
> use strict;
> use warnings;
> use File::Find;
>
> my [at] datas = ("test.mp3" , "only one brace here[anything.mp3" , "whatever.mp3");
>
> while ( [at] datas){
> my $ref = splice [at] datas,0,1;

That is usually written as:

my $ref = shift [at] datas;


> foreach ( [at] datas){
> if ($ref =~/$_/){

That doesn't test if $ref is a duplicate, it tests if $_ is a substring
of $ref, so this would print "This is a test mp3 file.html is a
duplicate\n":

if ( "This is a test mp3 file.html" =~ /test.mp3/ )

Because . will match any character and the pattern is not anchored.

If you want to see if the two strings are exactly the same then:

if ( $ref eq $_ ) {

Or you could use a hash instead of an array so you would know that there
are no duplicates.

But as to your question about the '[' character causing an error
messages, you have to use quotemeta to escape regular expressions
"special" characters:

if ( $ref =~ /\Q$_/ ) {


> print "$ref is a duplicate\n";
> }else{
> print "$ref is not a duplicate\n";
> }
> }
> }



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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ Mo, 13 Juni 2011 14:56 ] [ ID #2060995 ]

Re: how to use regexp to match symbols

On 2011-06-13 14:05, eventual wrote:

> I have a list of mp3 files in my computer and some of the file names consists of a bracket like this "darling I love [you.mp3"
> I wish to check them for duplicates using the script below, but theres error msg like this "Unmatched [ in regex; marked by<-- HERE in m/only one brace here[<-- HERE anything.mp3/ at Untitled1 line 13."

Why would you want to use a regex for this?

Use 'eq', or see 'perldoc -f index'.

In case of real regex need, see 'perldoc -f quotemeta'.

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
rvtol+usenet [ Mo, 13 Juni 2011 16:39 ] [ ID #2060997 ]

Re: how to use regexp to match symbols

> I have a list of mp3 files in my computer=A0and some of the file names co=
nsists of=A0 a bracket like this "darling I love [you.mp3"
> I wish to check them for duplicates using the script below, but theres er=
ror msg like this "Unmatched [ in regex; marked by <-- HERE in m/only one b=
race here[ <-- HERE anything.mp3/ at

Searching google I found that several scripts use the Find::Duplicates
Module. http://search.cpan.org/~tmtm/File-Find-Duplicates-1.00/lib/F ile/Fin=
d/Duplicates.pm

Sayth

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sayth Renshaw [ Mo, 13 Juni 2011 23:19 ] [ ID #2061000 ]
Perl » gmane.comp.lang.perl.beginners » how to use regexp to match symbols

Vorheriges Thema: Seeking opinions about YAPCs
Nächstes Thema: Trouble building File::BSDGlob.pm