LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-17-2005, 12:36 PM   #1
bwreath
LQ Newbie
 
Registered: Jul 2003
Posts: 16

Rep: Reputation: 0
problems with regular expression and grep


Hi,

I would appreciate help with the following.

if $userinput is the variable representing user input.

If the search term is

$newuserinput = "[^0123456789.:]".$userinput."[^0123456789.:]";

and within a php file I call

exec ($mydirectory."grep -E "newuserinput $mystring", $searchoutput, $searchstatus);

I have found that this grep command works most of the time except when
there is a space character in the variable $userinput.
What can I do to this expression to make it work when there are spaces
in the $userinput variable?

thanks!
 
Old 03-17-2005, 12:46 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I am curious why you don't just use the regular expression engine built into php... it would be much more efficient than a call to exec. If you still want to exec, you'll need to quote the regexp.
 
Old 03-17-2005, 01:05 PM   #3
bwreath
LQ Newbie
 
Registered: Jul 2003
Posts: 16

Original Poster
Rep: Reputation: 0
Hi Matir,

Thanks for your help. If I still want to use exec how would I quote the regexp

I am already using the following in my php file.
$newuserinput = "[^0123456789.:]".$userinput."[^0123456789.:]";

exec ($mydirectory."grep -E $newuserinput $mystring", $searchoutput, $searchstatus);

So would I change the line with the $newuserinput to:

$newuserinput = "[^0123456789.:]"."$userinput"."[^0123456789.:]";

ie. the $userinput variable input quoted?

thanks!
 
Old 03-17-2005, 02:24 PM   #4
bwreath
LQ Newbie
 
Registered: Jul 2003
Posts: 16

Original Poster
Rep: Reputation: 0
Actually, I tried quoting the $userinput variable recently. It doesn't work.

Any ideas?

$newuserinput = "[^0123456789.:]"."$userinput"."[^0123456789.:]";


thanks!
 
Old 03-17-2005, 02:54 PM   #5
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Quote:
Originally posted by bwreath
Actually, I tried quoting the $userinput variable recently. It doesn't work.

Any ideas?

$newuserinput = "[^0123456789.:]"."$userinput"."[^0123456789.:]";


thanks!
This solved a similar problem for me.
Code:
userinput=$(cat $userinput | tr " " "_")

$newuserinput="[^0123456789.:]"."$(cat $userinput | tr "_" " ")"."[^0123456789.:]";
You would have to replace "_" with a character that wont be in $userinput , of course.
 
Old 03-17-2005, 03:28 PM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
make your exec command like "grep '$userinput'" or similar. (Should quote it properly).
 
Old 03-18-2005, 01:43 PM   #7
bwreath
LQ Newbie
 
Registered: Jul 2003
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks Matir,

I used your suggestion and the grep worked properly.

also thanks ahh I used a modification of your idea for another part of my code
since spaces weren't allowed somewhere else
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with regular expression aecaudel Programming 6 11-04-2005 05:28 AM
Need help with Regular Expression subaruwrx Linux - Newbie 6 09-04-2004 07:48 PM
grep usage with regular expression jonathanztaub Linux - General 7 09-01-2004 10:35 PM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:51 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
Open Source Consulting | Domain Registration