tags:

views:

598

answers:

4

Simple question but I got a headache to solve this game. Example regex.

[a-zA-Z0-9\s]

[whitespace]Stack[whitespace]Overflow - not allow
Stack[whitespace]Overflow - allow
Stack[whitespace]Overflow[whitespace] - not allow

Let me know

Update regex from JG and it's working.

function regex($str)
{
    $check = preg_replace('/^[a-zA-Z0-9][a-zA-Z0-9\s]+[a-zA-Z0-9]$|^[a-zA-Z0-9]*$/', "", $str);

    if (empty($check)) {
        return true;
    } else {
        return false;
    }
}

$str = 'Stack Overflow ';
$validator = regex($str);

if ($validator) {
    echo "OK » " .$str;
} else {
    echo "ERROR » " . $str;
}
+1  A: 

From what I understood, you want a regex that doesn't allow your string to have either whitespace at the beginning, or at the end. Something along these lines should work:

/^[a-zA-Z0-9][a-zA-Z0-9\s]+[a-zA-Z0-9]$|^[a-zA-Z0-9]*$/

An example in Python:

import re
test = ["Stack Overflow",
        "Stack&!Overflow",
        " Stack Overflow",
        "Stack Overflow ",
        "x",
        "", 
        " "]
regex = re.compile(r'^[a-zA-Z0-9][a-zA-Z0-9\s]+[a-zA-Z0-9]$|^[a-zA-Z0-9]*$')
for s in test:
    print "'"+s+"'", "=>", "match" if regex.match(s) != None else "non-match"

Output:

'Stack Overflow' => match
'Stack&!Overflow' => non-match
' Stack Overflow' => non-match
'Stack Overflow ' => non-match
'x' => match
'' => match
' ' => non-match
JG
in a line that is invald, it will match parts of that line as valid. i think for this anything that doesn't explicitly use the ^ and $ characters won't work.
Victor
that's the beginning of line and end of line charcters, not how you're using it.
Victor
I was editing the post to include those..
JG
`[^\s]` will match any character that's not whitespace--including punctuation and control characters.
Alan Moore
`\S` does the same thing as `[^\s]`, so it's a bit silly using the later.
Daniel
+5  A: 

Try:

/^\S.*\S$|^\S$/

If you want only letters and numbers and underscores, and two words, no less, no more:

/^\w+\s+\w+$/

For no underscore,

/^\p{Alnum}+\s+\p{Alnum}+$/

Though, in some Regex styles (particularly PHP, which I see now is the target), you use this:

/^[[:alnum:]]+\s+[[:alnum:]]+$/

If any number of such words and numbers can be accepted:

/^\w[\w\s]*\w$|^\w$/
Daniel
this work! but how to allow only a-z A-Z 0-9?
bob
If you want to match those characters only, you cannot use \S as that matches every non-whitespace character. Instead, you must specify the characters that you want, just like I did in my answer.
JG
ok thanks JG :)
bob
\w includes the underscore, which he doesn't want.
JG
Indeed. Ok, ok, I'll fix it...
Daniel
A: 

if ($string =~ /^\S.*\S$/){ print "allowed\n"; } else { print "not allowed\n"; }

+2  A: 

Why on earth would you want to use regex for this?

trim (by default) removes:

*    " " (ASCII 32 (0x20)), an ordinary space.
* "\t" (ASCII 9 (0x09)), a tab.
* "\n" (ASCII 10 (0x0A)), a new line (line feed).
* "\r" (ASCII 13 (0x0D)), a carriage return.
* "\0" (ASCII 0 (0x00)), the NUL-byte.
* "\x0B" (ASCII 11 (0x0B)), a vertical tab.

so all you need is:

function no_whitespace($string)
{
      return trim($string) === $string;
}

And that is it!

$tests = array
(
    ' Stack Overflow',
    'Stack Overflow',
    'Stack Overflow '
);

foreach ($tests as $test)
{
   echo $test."\t:\t".(no_whitespace($test) ? 'allowed' : 'not allowed').PHP_EOL;
}

http://codepad.org/fYNfob6y ;)

JG