help a beginner

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
quasiperfect
Forum Newbie
Posts: 5
Joined: Mon Feb 18, 2013 4:30 am

help a beginner

Post by quasiperfect »

hi

i want to match strings with length min 1 max 10 characters composed by letters numbers and the character - (optional)
if present the character - can't be at start at end and don't allow double

and if is not to much please explain how you did it so i can understand and learn

thanks in advance for any help

edit

what i managed to do alone
^(?!-)[a-zA-Z0-9-]{1,10}(?<!-)$

but it doesn't catch double --
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: help a beginner

Post by requinix »

So "it starts with at least one letter or number, then there's any amount of (hyphen and then at least one more letter or number)".

I recommend strlen() to check the length, then

Code: Select all

/^[a-z0-9]+(-[a-z0-9]+)*$/i
quasiperfect
Forum Newbie
Posts: 5
Joined: Mon Feb 18, 2013 4:30 am

Re: help a beginner

Post by quasiperfect »

nope i will explain again
- it must contain at least one letter or number but no more then ten
- it can contain - but is optional , can't start with -, can't end with - and no double --
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: help a beginner

Post by s.dot »

I'm by no means a regex guys. Please take my post as trying to help rather than it being the solution, although it might be. :) What if you try to match the - one or more times?

Such as:
[text]^(?!-+)[a-zA-Z0-9-]{1,10}(?<!-+)$[/text]

The + means one or more times.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
quasiperfect
Forum Newbie
Posts: 5
Joined: Mon Feb 18, 2013 4:30 am

Re: help a beginner

Post by quasiperfect »

@s.dot tried that but won't catch double --
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: help a beginner

Post by s.dot »

quasiperfect wrote:@s.dot tried that but won't catch double --
Do you mean in the middle or at the beginning or end? If you mean in the midle, too.

[text]^(?!-+)[a-zA-Z0-9([^-]+)]{1,10}(?<!-+)$[/text]

That will be my last attempt because I'm literally just guessing. I am sorry. Hopefully someone more regex knowledgeable will chime in.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
quasiperfect
Forum Newbie
Posts: 5
Joined: Mon Feb 18, 2013 4:30 am

Re: help a beginner

Post by quasiperfect »

@s.dot yes i mean in the middle too

i tried to use your regex but RegexBuddy 3 doesn't like it

# These errors were found in your original regular expression:
# Closing round bracket found without a corresponding opening round bracket

# Your regular expression could not be converted to the flavor required by this language:
# The PCRE library does not support infinite repetition inside lookbehind
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: help a beginner

Post by requinix »

quasiperfect wrote:nope i will explain again
- it must contain at least one letter or number but no more then ten
- it can contain - but is optional , can't start with -, can't end with - and no double --
Clearly my comment about strlen() and the expression I wrote have been of no help whatsoever.
quasiperfect
Forum Newbie
Posts: 5
Joined: Mon Feb 18, 2013 4:30 am

Re: help a beginner

Post by quasiperfect »

@requinix i solved the problem using a combination of regex and php functions but i wanted a full regex method
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: help a beginner

Post by Christopher »

quasiperfect wrote:nope i will explain again
- it must contain at least one letter or number but no more then ten
- it can contain - but is optional , can't start with -, can't end with - and no double --
I don't think a single comprehensible regex will do that. I would recommend checking it step by step with clearly documented code. Probably a combination of regexs and looping through the string to do the counts. That is just too intricate to try to do in one shot.
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: help a beginner

Post by requinix »

It is possible but I really don't like doing it. The trick is to use assertions.

Code: Select all

/^(?=.{1,10}$)([a-z0-9](-[a-z0-9]+)*)$/i
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: help a beginner

Post by Christopher »

Yeah, I know. But does the average programmer really want to have to maintain that regex when the client asks for some tiny change in a year?
(#10850)
regexer
Forum Newbie
Posts: 4
Joined: Sat Dec 20, 2014 6:38 pm

Re: help a beginner

Post by regexer »

Christopher wrote:Yeah, I know. But does the average programmer really want to have to maintain that regex when the client asks for some tiny change in a year?
Unfortunately most programmers know only to write \d or .* in regexes .....
http://www.regex-builder.com/ online regular expression composer based on text analysis
phpRob
Forum Newbie
Posts: 9
Joined: Thu Feb 12, 2015 3:45 pm

Re: help a beginner

Post by phpRob »

(oops, I just solved it after writing a bunch...keeping it though due to some thoughts to consider)
I had been doing a good deal of regex lately and took a stab at your question. Kind of my version of a crossword puzzle. Not sure if you are doing your regex test on the Javascript- or the PHP-side. No matter really--yes, some syntaxes/techniques are not allowed on the js-side. If you are testing on the Javascript/client side, you could put two regex's in an onchange event-handler that returns true or false as to the success of a combination of two tests (provided below). If testing on the php side, testing with a method/function that uses string operations vs using a single statement regex test makes no matter of course, but I understand one would want the test to be robust yet concise just the same. I failed to enforce preventing multiple hyphens in a row without introducing a second test. Below, we want regExPattern1 to pass the test and regExPattern2 to fail the test. Sometimes dividing and conquering by breaking objectives down and solving on their own is the only way to go. Perhaps a single regex to test all objectives is possible; perhaps that single regex would be very difficult to maintain and modify without introducing a bug.

Code: Select all

$regExPattern1 = '/^[\da-zA-Z]{1}[\da-zA-Z\-]{0,9}(?<!\-)$/x';
$regExPattern2 = '/[\-]{2,}/';
This one solves what you asked for:

Code: Select all

$regExPattern = '/^[\da-zA-Z]{1}(?:[\d]|[a-z]|[A-Z]|(?<!\-)-){0,9}(?<!\-)$/';
Post Reply