LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   problems with regular expression and grep (https://www.linuxquestions.org/questions/linux-general-1/problems-with-regular-expression-and-grep-302818/)

bwreath 03-17-2005 12:36 PM

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!

Matir 03-17-2005 12:46 PM

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.

bwreath 03-17-2005 01:05 PM

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!

bwreath 03-17-2005 02:24 PM

Actually, I tried quoting the $userinput variable recently. It doesn't work.

Any ideas?

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


thanks!

ahh 03-17-2005 02:54 PM

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.

Matir 03-17-2005 03:28 PM

make your exec command like "grep '$userinput'" or similar. (Should quote it properly).

bwreath 03-18-2005 01:43 PM

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


All times are GMT -5. The time now is 04:07 PM.