Forum Moderators: coopster

Message Too Old, No Replies

Regex with 2 character minimum problem

         

neophyte

12:37 pm on Jun 2, 2015 (gmt 0)

10+ Year Member



Hello All -

I've been hassling over this for 3 hours and just can't get it right.

The point is crafting a regular expression that:
1. Allows alphanumeric input;
2. An OPTION of one or more embedded hyphens (hyphens can delimit the alphanumeric input but can't be leading or trailing).
3. The input must be a be a minimum of 2 alphanumeric characters long such as: ab or a-b.

So far I've been able to cobble together the following (from many examples found on the web) which satisfies requirements 1 and 2:

'/^([a-z0-9]+-)*[a-z0-9]+$/i'

What I can't figure out is where to place the {2,}.

Hoping that someone can guide me on how to achieve this last two-character requirement.

whitespace

1:36 pm on Jun 2, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



My initial thought was, yes #3 would seem to be a problem for regex. You can't count just "alphanumeric" characters. So, you could split it into two patterns: Either 2 or more alphanumeric OR your current pattern (which is 3 or more chars). (?)

However, what about the following...


([a-z0-9]+-?)+[a-z0-9]+


The hyphen is optional and the first sub pattern must be repeated 1 or more times? So presumably you can have something like "aa-b-ccc-d"? A hyphen cannot be next to another hyphen.

lucy24

6:36 pm on Jun 2, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



1. Allows alphanumeric input;
2. An OPTION of one or more embedded hyphens (hyphens can delimit the alphanumeric input but can't be leading or trailing).
3. The input must be a be a minimum of 2 alphanumeric characters long such as: ab or a-b.

Do you expressly need to exclude _ lowlines? If not, "alphanumeric" can be concisely expressed as \w ("word character").

You said "delimit", but it looks as if you meant the opposite: "can occur between, BUT NOT at the boundaries". Also: do you expressly need to exclude -- sequences? I'm assuming they simply wouldn't occur, in which case you don't need to allow for them in the code.

Are you evaluating a complete string, or just a section of it such as one directory? It's a question of whether you use ^ and $ anchors, or literal characters such as / :
/\w[\w-]*\w/
If you do need to exclude multi-hyphens (say, -- occurs in other directories that you don't want to match):
/(?:\w+-?)+\w+/
There will be a final single-character hiccup on the last \w but that's no biggie.

neophyte

12:24 am on Jun 3, 2015 (gmt 0)

10+ Year Member



Hello Whitespace and Lucy24 -

Thanks very much for your input.

Whitespace - have tried your regex and works fine for my needs... your assistance is greatly appreciated.

Lucy24 - Thanks very much for your sample as well... will keep that on file for future reference