LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Regex Help Pleae (https://www.linuxquestions.org/questions/programming-9/regex-help-pleae-587625/)

nomb 09-26-2007 07:21 PM

Regex Help Pleae
 
Hey guys,

I have an almost working solution, however it isn't perfect...

I could I pull say 'PermitRootLogin Yes' out of a conf file?
Right now, I have:

'/((#\s*)PermitRootLogin(\s*)?Yes)(\s*#.*)?/i'

This works ok but not very good. Here is a better example:

'/((#\s*)Port(\s*)22)(\s*#.*)?/i'

This regex is suppose to search to see ifyou have 'Port 22' in the conf file. However, even if you don't have 22 but another port it still trigures.

Can anyone help me please?

Thanks,

nomb

By The Way: I'm using preg_match under php.

bartonski 09-26-2007 10:01 PM

Can you post a few lines which trigger, but are not using port 22?

By the way, if you're debugging regular expressions, I recommend regex-coach

sundialsvcs 09-26-2007 10:16 PM

Voice of experience ...

When you are designing a regular-expression to look for a particular thing, take care that you devise the regexp only to detect "what you are looking for," not "whatever surrounds it."

nomb 09-28-2007 09:02 AM

Thats the problem. I can design the regex to match exactly what I want without problems. The problems occur when the line is commented out with # followed by any number of spaces... Or isn't the first line in the file. Or has comments on the same line after the conf option...

This is what I'm using for detecting port 22:

Code:

if (!(preg_match('/((#\s*)Port(\s*)22)(\s*#.*)?/i', $ssh_config, $array ))) {
  echo "<font color='yellow'>CAUTION!!!</font> - The <b>Port 22</b> setting sets the SSHd to run on the default port.  If you have your server setup correctly this shouldn't be a problem.  However, it is never a bad idea to change the default port to something else.<br>";
  echo "<b>You currently have:</b> <font color='yellow'>$array[0]</font><br><br>";
}

Again thanks for helping me,
nomb

ghostdog74 09-28-2007 09:23 AM

show a sample of input file.

nomb 09-28-2007 11:59 AM

The input file is just the sshd_conf file.

nomb 09-28-2007 01:04 PM

The Regex-coach is alright, altho All I had to do was enter
'Port(\s*)22' as the expression and it matched it correctly in
the middle of the file. But php wouldn't it like that because
there was things before it and after it. Plus the expressions
is missing the carriage returns at the end as well.

nomb

ghostdog74 09-28-2007 07:46 PM

i don't really get what you are trying to achieve. if you just want to get the line with "PermitRootLogin" with a "yes" value
Code:

awk '/PermitRootLogin/{ if ($2~/[yY]es/)print }' sshd_config
if you want to search whethere there's port 22,
Code:

awk '/Port 22/' sshd_config

nomb 10-01-2007 09:53 AM

I'm making a web app to check config files using regex...
Therefore awk isn't a good solution for me...

bartonski 10-02-2007 06:31 AM

Quote:

Originally Posted by nomb (Post 2906770)
The Regex-coach is alright, altho All I had to do was enter 'Port(\s*)22' as the expression and it matched it correctly in the middle of the file. But php wouldn't it like that because there was things before it and after it. Plus the expressions is missing the carriage returns at the end as well.

nomb

AFAIK, php should work just like Perl, in terms of regular expressions: you specify what you want to match, and don't worry about what doesn't match.

Also, you don't need the parentheses: they're used for capturing backreferences, so, for example

Code:

/Port(\s*)22/
will match Port 22 and put a space in $1

What you're actually looking for is

Code:

/Port\s+22/
This will detect one or more spaces between "Port" and "22", but doesn't put anything in a backreference.

my guess is that you want something like this:

Code:

unless(/^#/) {
        if(/Port\s+22/){
                  # Print Statements Here
        }
}


chrism01 10-02-2007 07:24 PM

Actually, PHP has 2 regex engines, it's own, used as above, and the Perl Compatible Regex Engine aka pcre() fns.
http://au2.php.net/manual/en/ref.pcre.php

nomb 10-03-2007 08:39 AM

Quote:

Originally Posted by bartonski (Post 2910323)
my guess is that you want something like this:

Code:

unless(/^#/) {
        if(/Port\s+22/){
                  # Print Statements Here
        }
}


This is what I ended up with. Can you rewrite it a little cleaner for me?

Code:

if ((preg_match('/(#\s*)Port(\s*)[0-9]*(\s*#.*)?/i', $ssh_config, $array ))) {
  Do whatever here...
}

elseif ((preg_match('/Port(\s*)22(\s*#.*)?/i', $ssh_config, $array ))) {
  Do whatever here...
}

Now the only problem is that if someone has the defaults commented out, and then their own, it only pulls in whichever is first...

Like:

#port 22
#port 333

Would still pull #port 22

nomb

Mcribs 10-03-2007 09:09 AM

I'm not sure how PHP's regex symbols are; but under perl there is the '^' symbol for saying that the line must begin with whatever regex comes after it.

For example, in Perl I could say /^\s*Port\s+22/ and this would match any string that started with 0 or more spaces, followed by the word Port followed by 1 or more zeros and then 22.

I don't know the conversion to PHP but if you get the idea it shouldn't be that hard.

nomb 10-03-2007 07:09 PM

Ya I tried that but by using the ^, if the line I was searching for wasn't the very first line or had anything before it then it wouldn't find it. Same thing with the $. If the line had anything after it or any carriage returns or anything it wouldn't find it. Thats why I'm not using either of them.

Mcribs 10-03-2007 10:01 PM

Can you be a little more specific when you say line? Like do you mean string when you say line?

I ask because in your last reply say if it wasn't in the first line. That would imply that you are trying to match the regular expression to the entire file, which may or may not be what you want.


All times are GMT -5. The time now is 03:42 AM.