Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Filtering a string

Reply
Thread Tools

Filtering a string

 
 
Bill H
Guest
Posts: n/a
 
      06-20-2008
Can someone point me to some docs on how I would do this without
iterating over the whole string (pattern matching?):

$original = "a malformed%string/containi\"ng characters I don'~t
want! ...";

$filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";

$new = &fix($original);

$new would now equal:

amalformedstringcontainingcharactersidontwant

sub fix
{
my $o = shift;
my $r = "";
my $i = 0;
for($i = 0;$i < length($o);$i++)
{
if(index($filter,uc(substr($o,$i,1))) != -1){$r .= substr($o,$i,1);}
}
return($r);
}

I just typed this in to give you the gist of what I want to do so if
there are errors it is in my typing.

Bill H
 
Reply With Quote
 
 
 
 
Dave B
Guest
Posts: n/a
 
      06-20-2008
Bill H wrote:

> Can someone point me to some docs on how I would do this without
> iterating over the whole string (pattern matching?):
>
> $original = "a malformed%string/containi\"ng characters I don'~t
> want! ...";
>
> $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
>
> $new = &fix($original);
>
> $new would now equal:
>
> amalformedstringcontainingcharactersidontwant


Is the following acceptable for you?

$new = lc(join("",grep(m/[$filter]/i,split(//,$original))));

--
D.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      06-20-2008

Quoth Bill H <(E-Mail Removed)>:
> Can someone point me to some docs on how I would do this without
> iterating over the whole string (pattern matching?):
>
> $original = "a malformed%string/containi\"ng characters I don'~t
> want! ...";
>
> $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
>
> $new = &fix($original);
>
> $new would now equal:
>
> amalformedstringcontainingcharactersidontwant


$orginal =~ tr/a-zA-Z0-9_-//cd;

See tr/// under 'Regexp Quote-Like Operators' in perlop.

Note that '-' must come first or last, as otherwise it will be
interpreted as part of an X-Y range.

Ben

--
For far more marvellous is the truth than any artists of the past imagined!
Why do the poets of the present not speak of it? What men are poets who can
speak of Jupiter if he were like a man, but if he is an immense spinning
sphere of methane and ammonia must be silent? [Feynmann] http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      06-20-2008
Bill H wrote:
> Can someone point me to some docs on how I would do this without
> iterating over the whole string (pattern matching?):
>
> $original = "a malformed%string/containi\"ng characters I don'~t
> want! ...";
>
> $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
>
> $new = &fix($original);
>
> $new would now equal:
>
> amalformedstringcontainingcharactersidontwant


$ perl -le'
$original = "a malformed%string/containi\"ng characters I don\047~t
want\041 ...";
$filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";

( $new = $original ) =~ s/[^\Q$filter\E]+//ig;
print for $original, $new;
'
a malformed%string/containi"ng characters I don'~t want! ...
amalformedstringcontainingcharactersIdontwant



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
Bill H
Guest
Posts: n/a
 
      06-21-2008
On Jun 20, 1:48*pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth Bill H <(E-Mail Removed)>:
>
> > Can someone point me to some docs on how I would do this without
> > iterating over the whole string (pattern matching?):

>
> > $original = "a malformed%string/containi\"ng characters I don'~t
> > want! ...";

>
> > $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";

>
> > $new = &fix($original);

>
> > $new would now equal:

>
> > amalformedstringcontainingcharactersidontwant

>
> $orginal =~ tr/a-zA-Z0-9_-//cd;
>
> See tr/// under 'Regexp Quote-Like Operators' in perlop.
>
> Note that '-' must come first or last, as otherwise it will be
> interpreted as part of an X-Y range.
>
> Ben
>
> --
> For far more marvellous is the truth than any artists of the past imagined!
> Why do the poets of the present not speak of it? What men are poets who can
> speak of Jupiter if he were like a man, but if he is an immense spinning
> sphere of methane and ammonia must be silent? [Feynmann] * * (E-Mail Removed)


Ben this one seems to work nice for me, only issue is I have to lc()
the results (not really a problem). Is there a way to have that done
in this ? Also thanks for pointing out the part in perlop that deals
with it.

Bill H
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-21-2008

Quoth Bill H <(E-Mail Removed)>:
> On Jun 20, 1:48*pm, Ben Morrow <(E-Mail Removed)> wrote:
> > Quoth Bill H <(E-Mail Removed)>:
> >
> > > Can someone point me to some docs on how I would do this without
> > > iterating over the whole string (pattern matching?):

> >
> > > $original = "a malformed%string/containi\"ng characters I don'~t
> > > want! ...";

> >
> > > $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";

> >
> > > $new = &fix($original);

> >
> > > $new would now equal:

> >
> > > amalformedstringcontainingcharactersidontwant

> >
> > $orginal =~ tr/a-zA-Z0-9_-//cd;
> >
> > See tr/// under 'Regexp Quote-Like Operators' in perlop.
> >
> > Note that '-' must come first or last, as otherwise it will be
> > interpreted as part of an X-Y range.

>
> Ben this one seems to work nice for me, only issue is I have to lc()
> the results (not really a problem). Is there a way to have that done
> in this ?


Well, tr/-_a-zA-Z0-9\000-\177/-_a-za-z0-9/d will work, but personally
I'd probably just use lc, and probably beforehand, to avoid two
alphabetic ranges. Something like

(my $new = lc $original) =~ tr/a-z0-9_-//cd;

seems clearer than relying on the details of tr///, which is an operator
a lot of Perl programmers don't know very well.

Ben

--
Outside of a dog, a book is a man's best friend.
Inside of a dog, it's too dark to read.
(E-Mail Removed) Groucho Marx
 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      06-23-2008
On Fri, 20 Jun 2008 19:44:37 +0200, Dave B <(E-Mail Removed)> wrote:
> Bill H wrote:
>
> > Can someone point me to some docs on how I would do this without
> > iterating over the whole string (pattern matching?):
> >
> > $original = "a malformed%string/containi\"ng characters I don'~t
> > want! ...";
> >
> > $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
> >
> > $new = &fix($original);
> >
> > $new would now equal:
> >
> > amalformedstringcontainingcharactersidontwant

>
> Is the following acceptable for you?
>
> $new = lc(join("",grep(m/[$filter]/i,split(//,$original))));
>


That won't do what the OP wants - try it with "=", "[" or "<" in
$original, for example.


 
Reply With Quote
 
Dave B
Guest
Posts: n/a
 
      06-23-2008
Dave Weaver wrote:

> On Fri, 20 Jun 2008 19:44:37 +0200, Dave B <(E-Mail Removed)> wrote:
>> Bill H wrote:
>>
>>> Can someone point me to some docs on how I would do this without
>>> iterating over the whole string (pattern matching?):
>>>
>>> $original = "a malformed%string/containi\"ng characters I don'~t
>>> want! ...";
>>>
>>> $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
>>>
>>> $new = &fix($original);
>>>
>>> $new would now equal:
>>>
>>> amalformedstringcontainingcharactersidontwant

>> Is the following acceptable for you?
>>
>> $new = lc(join("",grep(m/[$filter]/i,split(//,$original))));
>>

>
> That won't do what the OP wants - try it with "=", "[" or "<" in
> $original, for example.


Yes, the "-" in the filter should be at the very beginning or end, I
overlooked that. However, after seeing the solutions based on tr///, I
realized that this is not by any means a good solution. I'm still learning...

--
D.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass Query String Value to XSL for filtering Corey XML 4 02-16-2005 02:03 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments