Jump to content

How do you include a Space in Regex?


doubledee

Recommended Posts

In your regex, you can use "\s" in your regex as a whitespace character. Don't forget you can also trim() your values prior to going through regex, as that can greatly reduce some of the necessary leading/trailing whitespace logic in your regular expression.

Link to comment
Share on other sites

In your regex, you can use "\s" in your regex as a whitespace character. Don't forget you can also trim() your values prior to going through regex, as that can greatly reduce some of the necessary leading/trailing whitespace logic in your regular expression.

 

Okay, maybe this sounds obvious, but I meant spaces in between characters (e.g. "H E L L O").

 

Or is it understood that words can have spaces?

 

I just assumed my regex above would mean everything has to be contiguous with no spaces in between characters?!  :shrug:

 

 

 

Debbie

 

 

Link to comment
Share on other sites

Just add space:

 

if (preg_match('/^[A-Z0-9#&\',. -]{2,30}$/i', $_POST['address1'])){

 

Or use \s like SamT_ suggested.

 

Is that a SPACE I see between the period and hyphen at the end??  :confused:

 

So using \s is just as good?  (I guess it depends on your answer to my question above.)

 

Also, if I do use \s, does it matter where it goes?  (I found out tonight that if you don't put the hyphen last that things don't work?!  (Why is that???)

 

Thanks,

 

 

 

Debbie

 

Link to comment
Share on other sites

- Yes, it's a space.

- \s means whitespace. That includes spaces, tabs, and newline characters (and a few more).

- A hyphen in a character set (the [...]) means a range when put between two characters. It's how the A-Z and 0-9 work. If you want a literal hyphen, escape it (with a backslash) or put it at the beginning or end of the set (where it can't possibly mean a range so it loses the special meaning).

 

Allowing spaces will mean that there can be any number of them anywhere in the string. It will allow "HELLO", "H E L L O", and

H   E      L         L            O

If that's not what you want, be very specific about what you want to allow and what you do not want to allow.

Link to comment
Share on other sites

- Yes, it's a space.

- \s means whitespace. That includes spaces, tabs, and newline characters (and a few more).

- A hyphen in a character set (the [...]) means a range when put between two characters. It's how the A-Z and 0-9 work. If you want a literal hyphen, escape it (with a backslash) or put it at the beginning or end of the set (where it can't possibly mean a range so it loses the special meaning).

 

Allowing spaces will mean that there can be any number of them anywhere in the string. It will allow "HELLO", "H E L L O", and

H   E      L         L            O

If that's not what you want, be very specific about what you want to allow and what you do not want to allow.

 

Upon reflection, this is trickier than it looks...

 

I had my First Name regex like this...

 

preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['firstname']

 

If I add a SPACE in between Z and \' then my understand is that someone could enter 2-20 SPACES, or

H  E      L  L    O
which is not what anyone wants?!

 

So how do I write a regex that says, "Accept A-Z, apostrophe, period, hyphen and one and only one space between the previous characters"??

 

That would allow for:

 

Mary Jane

J. P.

Y Not

 

and eliminate nonsensical entries.

 

Thanks,

 

 

Debbie

 

 

Link to comment
Share on other sites

and one and only one space between the previous characters

So any number of spaces in the whole thing but no two consecutive spaces?

I'd keep the simpler regex you have now (where you just added the space) and add a second check with strpos.

$valid = (preg_match($pattern, $string) && strpos($string, "  ") === false);

Just because regular expressions are powerful doesn't mean you have to use one to do everything ;)

 

@SamT_: Mmm, Bad Apple...

Link to comment
Share on other sites

and one and only one space between the previous characters

So any number of spaces in the whole thing but no two consecutive spaces?

I'd keep the simpler regex you have now (where you just added the space) and add a second check with strpos.

$valid = (preg_match($pattern, $string) && strpos($string, "  ") === false);

Just because regular expressions are powerful doesn't mean you have to use one to do everything ;)

 

@SamT_: Mmm, Bad Apple...

 

I've been accused of over-analyzing things!!  ;)

 

What I was thinking is that my original Regex would allow someone to enter 20 contiguous spaces, right?

 

Or something like

"I          am        a      bad  per      son"
.

 

 

 

Debbie

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.