Pattern Matching

Posted on 1998-12-28
Last Modified: 2010-03-05
How do I match a string for multiple characters?

$string = "sgds*G$GJ#G()g";

I want to search $string for any instances of * OR # OR $.

What's the easiest (best) way to do this?

Question by:tribal9
LVL 84

Expert Comment

ID: 1207184
Do you mean
$string = 'sgds*G$GJ#G()g';
or did you define $GJ
What do you want to do with any instances of * OR # OR $
To check if $string contains any of them, you might say:
print '$string contains instances of * OR # OR $' if $string =~ /[*#\$]/;
or perhaps:
$string =~ m'[*#$]'

Expert Comment

ID: 1207185
Is this what you want?

if ($string =~ /[\*\$#]/) {
  print "Found one!";
else {
  print "Sorry!";


Accepted Solution

comvosora earned 20 total points
ID: 1207186
Well, you can do a

@a = $string =~ /.*([#*$])+.*/gsi;

and get each occurence in one item of array @a (if you would like to count them, for instance)

Author Comment

ID: 1207187
Ah, yes, Martin and Ozo, that was what I wanted.

The $G in the string was a typo (meant just the '$' char, not variable), and I am using it to block out unacceptable names from a form submission.

LVL 84

Expert Comment

ID: 1207188
martinag's solution works, (although it's not neccessary to backwhack the * inside of a [] character class)
But the Accepted Answer does not work.
(and even with a \ in front of the $, the .* would mean it only gets the last occurance, contrary to comvosora's comment)

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now