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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search or regex question

Status
Not open for further replies.

sedawk

Programmer
Feb 5, 2002
247
US
I have these few lines as an example:

03#
1#
3#

The goal is to pull the first character each line given the rest of the line as a search key. For example, if given $search_key=3# in line 1, search result shows 0. If given $search_key=#, search results should show only the 2nd and 3rd lines. Using

if (/$search_key/) ...

doesn't work 'cause it won't differentiate # and 3# when $search_key=# so all 3 lines appear. On the other hand, $search_key=3# only shows the first line, which is correct.

Before I try to use substr, I want to make sure if there are other ways to do it.

Thanks.
 
Instead of "==" you could use "eq".

Regex could be used as well.
































dmazzini
GSM/UMTS System and Telecomm Consultant

 
Code:
if (/^(.)\Q$search_key\E$/) {

That regexp will allow for only one character to appear at the very beginning of the string followed by the quoted search key (so $search_key can contain regexp special characters and they're automatically escaped so ya don't get syntax errors) and then that has to be at the very end of the string.

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top