LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 08-27-2012, 09:58 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 540

Rep: Reputation: 32
C++ find or regex


Hi, if all I'm interested in is determining whether a substring exists within a longer string, and not where this substring is found in the longer string, should I use the C++ find function (documented in following URL), or use a regex strictly from a computational speed perspective?
http://www.cplusplus.com/reference/string/string/find/
 
Old 08-27-2012, 11:25 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,682

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by grob115 View Post
Hi, if all I'm interested in is determining whether a substring exists within a longer string, and not where this substring is found in the longer string, should I use the C++ find function (documented in following URL), or use a regex strictly from a computational speed perspective?
http://www.cplusplus.com/reference/string/string/find/
grep might do the job.
Examples:
Code:
echo "thisisaverylongstring" \
|grep 'verylong'

echo "thisisafairlylongstring" \
|grep 'verylong'
Daniel B. Martin

Last edited by danielbmartin; 08-27-2012 at 11:25 AM. Reason: Correct t7po
 
Old 08-27-2012, 11:33 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,369

Rep: Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418
if you need to match only exact string (without real regexp) you should use find and not regex. You will check the return code. Probably strstr is even faster: http://www.cplusplus.com/reference/c...string/strstr/
But actually you can write a small test code to check, find is quicker.
 
  


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
[SOLVED] Regex to find words with repeated letters divyashree Programming 3 03-24-2012 09:35 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
[SOLVED] find with a regex path bdoremus Programming 7 05-05-2010 10:24 PM
Quick regex problem, can't find solution R00ts Programming 3 05-25-2005 02:55 PM

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

All times are GMT -5. The time now is 06:37 PM.

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