Nils
Nils

Reputation: 189

Easiest validation regular expression not matching space at beginning or end

I'm trying to find the easiest validation regular expression (PCRE) for use in method preg_match() in PHP. I want to keep it as easy as possible and avoid repetition if possible.

My matching criteria in words is:

My regular expression knowledge might be lacking but what I come up with without the second space criterion is:

    /^[a-zA-Z0-9 +&-]+$/

To not match space I'm thinking about something like

    /^[^ ]+[a-zA-Z0-9 +&-]+[^ ]+$/

but this actual piece would need 3+ characters.

If I do

    /^[^ ]*[a-zA-Z0-9 +&-]+[^ ]*$/

it will not work at all times either, I suppose it has to do with the "greediness" of the middle part, but I've really tried to research how to get it right without succeeding.

Thankful for any kind of advice or pointer in the right direction!

Upvotes: 0

Views: 2366

Answers (4)

user557597
user557597

Reputation:

I would put focus on what is wanted.

^(?i)[a-z0-9+&-][a-z0-9 +&-]*(?<=[a-z0-9+&-])$

Upvotes: 0

Marc B
Marc B

Reputation: 360762

Your main character class includes a space character, so even though you explicitly exclude spaces with the [^ ]* portion, you still ALLOW spaces with your main [a-z...], so you've effectly negated the entire purpose of the regex.

basically, you've put up a no parking sign that says "no parking anytime. parking allowed 9-5".


followup: What you want are negative assertions:

/^(?<!\s)[a-z.....](?>!\s)$/

The first one is a negative (!) look-behind (<) assertion that says "do not allow a whitespace (\s) before whatever follows ([a-z...]). The other one is the same, but is a negative look-ahead (>).

Upvotes: 0

Thorbear
Thorbear

Reputation: 2343

It seems you don't want to just be given a pattern so I'll try to give some pointers instead.

You want to match a string that begins with any character from the list [a-zA-Z0-9+&-], you want it to be followed by any character from that same list or a space, for an unlimited length.

To make the pattern as short as possible, you can remember that * matches from 0 to unlimited times, which means that whatever you put in front of it, does not actually have to appear there at all; the pattern (ab*)+ can match ab or abab or aaa but never ba

Upvotes: 0

mario
mario

Reputation: 145482

You want to wrap both [^ ] conditions into assertions. Lefthand (?=) and (?<=) at the end.

 /^(?=[^ ])[a-zA-Z0-9 +&-]+(?<=\S)$/

I think it's sufficient if you test just for one non-space character on each end. Then it's already ensured the content begins with a letter or another allowed character.

See http://www.regular-expressions.info/lookaround.html for a nice explanation.

Upvotes: 2

Related Questions