how to use regexp to match symbols
how to use regexp to match symbols
am 13.06.2011 14:05:39 von eventual
--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."
=0ASo how do I rewrite th=
e regexp.=0AThanks.
=0A###### script ###=0A#!/usr/bin/perl=0Ause stric=
t;=0Ause warnings;=0Ause File::Find;
=0Amy @datas =3D ("test.mp3" , "o=
nly one brace here[anything.mp3" , "whatever.mp3");
while (@datas){=0A=
=A0 my $ref =3D splice @datas,0,1;
foreach (@=
datas){
if ($ref =3D~/$_/){
=
=A0 print "$ref is a duplicate\n";
=A0=
=A0 }else{
=A0 print "$ref is not =
a duplicate\n";
}
}=0A}
--0-1914841569-1307966739=:16504--
Re: how to use regexp to match symbols
am 13.06.2011 14:14:20 von Rob Coops
--0016e64ea22a1dba4104a596ddb5
Content-Type: text/plain; charset=UTF-8
On Mon, Jun 13, 2011 at 2:05 PM, eventual 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 @datas = ("test.mp3" , "only one brace here[anything.mp3" ,
> "whatever.mp3");
>
> while (@datas){
> my $ref = splice @datas,0,1;
> foreach (@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 hereanything.mp3" which would mean you never
close the group and thus the regular expression is invalid and will throw an
error.
Regards,
Rob
--0016e64ea22a1dba4104a596ddb5--
Re: how to use regexp to match symbols
am 13.06.2011 14:56:18 von jwkrahn
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 @datas = ("test.mp3" , "only one brace here[anything.mp3" , "whatever.mp3");
>
> while (@datas){
> my $ref = splice @datas,0,1;
That is usually written as:
my $ref = shift @datas;
> foreach (@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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to use regexp to match symbols
am 13.06.2011 16:39:57 von rvtol+usenet
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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how to use regexp to match symbols
am 13.06.2011 23:19:28 von Sayth Renshaw
> 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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/