LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2004, 01:46 AM   #1
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
Perl variable string search


How do I search for the value in a string scalar in another string?

eg.

$foo = "Long long ago";
$bar = "ago";

How do I search for $bar in $foo? I can't just use a regexp s/ search because $bar might potentially contain regex metacharacters. Is there a any function which can escape the regexp in $bar or an alternate strstr like function which I can use?
 
Old 05-07-2004, 09:19 PM   #2
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint Debian Edition (LMDE)
Posts: 215

Rep: Reputation: 31
You can use the index function. Looks like it doesn't care about or recognize regular expressions.

Code:
#!/usr/bin/perl

$foo = "Long Long ago";
$bar = "ago";

$position = index( $foo , $bar );

print( "Found in position $position\n" ) if $position >= 0;
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C....Search a string for a string Scrag Programming 4 06-14-2004 05:15 PM
search for my string alaios Linux - General 6 05-08-2004 02:32 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM
variable to string x2000koh Programming 4 07-30-2003 03:23 AM
Getting a variable name based on a string. jtshaw Programming 7 10-08-2002 03:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration