开发者

Easiest validation regular expression not matching space at beginning or end

开发者 https://www.devze.com 2023-04-13 08:49 出处:网络
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.

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:

  • Allow one or more characters (this implies string should be 1 characters and up in total) from the following list:

    a-zA-Z0-9 +&-
    
  • Do not allow space in beginning or end

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 point开发者_Go百科er in the right direction!


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.


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


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 (>).


I would put focus on what is wanted.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消