Solved

Pattern Matching

Posted on 1998-12-28
5
126 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
5 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
Is this what you want?

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

Martin
0
 

Accepted Solution

by:
comvosora earned 20 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

9 Experts available now in Live!

Get 1:1 Help Now