Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pattern Matching

Posted on 1998-12-28
5
Medium Priority
?
138 Views
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?

Thanks-
Matt
0
Comment
Question by:tribal9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 84

Expert Comment

by:ozo
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'[*#$]'
0
 
LVL 4

Expert Comment

by:martinag
ID: 1207185
Is this what you want?

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

Martin
0
 

Accepted Solution

by:
comvosora earned 80 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)
0
 

Author Comment

by:tribal9
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.

0
 
LVL 84

Expert Comment

by:ozo
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)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans

715 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