Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Pattern Matching 1

Status
Not open for further replies.

phbills

Technical User
Mar 4, 2002
3
US
Hi all,

What I need to do is match all alphanumeric characters from a string variable, and assign the results to a variable. More than likely a simple task for someone well versed in Perl, myself not included.

Thanks.
 
Yes, it is. What do you want to do with the characters that aren't alphanumeric? If you expect us to do it all for you, you could expend a bit more effort on your requirements specification...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
That having been said, I think the answer would be something like:
Code:
my $sString = "AAA+++bbb";

$sString =~ /\W//g; # Delete all non-alfanumeric characters

print $sString; # Will print "AAAbbb"
 
It is one possible way (altough it will also allow underscores, not just alpha-numeric characters), but we try and not post code until people asking questions have shown some effort, mostly because they tend to be students. You are of course free to post anything you want since that is not a forum rule, just an informal policy nearly everyone that answers questions on this forum tries to observe. If you don't want to observe that informal policy that is up to you. I sometimes will post code for a person that has shown no effort if the question is an interesting one. This one is very simple however and any basic regexp tutorial should have given them enough information to solve this problem.

Anyway, a more efficient method if there are no other requirements:

Code:
$foo = 'abc123+++xyz_&^%$456';
($bar = $foo) =~ tr/a-zA-Z0-9//dc;
print $bar;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you MacTommy and Kevin ADC. I do understand students sometimes use boards to do their homework. I am only a student of life and hard knocks who hopes to return and complete a BBM in coming years.

The script I'm working on uses exiftool.exe, dcm2jpg.exe, and putty to convert DICOM images into jpgs, and then move them to another server. My problem lies in the fact that end users, Radiology Technicians, sometimes find it useful to annotate the PatientsName field of the DICOM header with fun characters: , ( * ^. After the PatientsName and StudyID field is returned by exiftool, a directory is created to store the jpg images created later by dcm2jpg ("$name.$id"). If $name has characters that dcm2jpg can't handle, the script terminates. Thus my need to eliminate all non-alphanumeric characters from $name before passing to dcm2jpg.

Again, thanks MacTommy and KevinADC for the help, and thanks to stevexff for keeping the forums honest.

++KevinADC;
 
Fair enough, good luck with the project.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Fair enough indeed.

And just for my enlightment..., why would
Code:
$string =~ tr/a-zA-Z0-9//dc
be more efficient than what I had (or intended to have I just noticed...):
Code:
$string =~ s/\W//g

I mean, apart from the slight differences in the character classes, and the fact that you copy the string (which isn't necessarily more efficient if you don't need the original one afterwards, I'ld say...), is there any reason why tr// would be better than s//..?!?


 
There is no need to copy to a new variable, I thought that was what you wanted to do. But it can be done like this:

Code:
$foo = 'abc123+++xyz_&^%$456';
$foo =~ tr/a-zA-Z0-9//dc;
print $foo;

tr/// is more efficient than s/// because it is less powerful. tr/// has few options and doesn't do any of the more exotic stuff that m// or s/// can do. That is what makes it more efficient. In fact, it can be orders of magnitude more efficient than s///.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top