homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Regex issue

 6:19 pm on Apr 2, 2013 (gmt 0)

Hi there people of WebmasterWorld!

Simple regex for those who know what to do....

Input string(s) to match:-


Regex pattern:-


Why Oh Why doesn't it function?

I swear that the {3,4,5} part means 3 or 4 or 5 of the preceding range.

please point me in the right direction.




 7:29 pm on Apr 2, 2013 (gmt 0)

^ASH[0*](34|45)$ is not perfect, but gets there


 7:41 pm on Apr 2, 2013 (gmt 0)

Does your string always start "ASH", as in your examples?


The first part of your regex matches a lot more than just "ASH". [ASH] denotes a character class, so any 1 of these characters are permitted. And then you are stating that there must be 3 of these. So, "AAA", "SSS", "HHH", "ASS", etc. etc. all match.


AFAIK {3,4,5} is invalid. The curly brace 'range' can take at most 2 values, a minimum and a maximum. So, this should simply be {3,5}

So, assuming that it should start "ASH" then try...

\d is shorthand for [0-9], matching digits only.


 7:53 pm on Apr 2, 2013 (gmt 0)


It seems that I've partially misunderstood something in the logical side of the processing. I've tried my original code but instead of {3,4,5} I've done {4,5} which works; SO, its obvious to me now that doing: this || that || the other doesn't function because you can only do or (||) comparision on two variables, I was trying to do it on three.

Thanks for the idea there @jlnaman, though that doesn't do what I needed.


Hi there Penders,

Thanks for the indepth look at my problem, and yes, by the looks of it, you're completely right. I've looked at my existing code and I seem to have the same error in a few other places.

So thanks for pointing my problem out, and as for the 2 chars between braces, yeah, I worked that out just now.

Thanks anyway.


[edited by: Matthew1980 at 8:09 pm (utc) on Apr 2, 2013]


 7:58 pm on Apr 2, 2013 (gmt 0)

Just a thought, are you matching the exact numeric parts... "00045", "0034" or "034" as in your examples and as jlnaman assumes, or a rather more generic 3 to 5 digit number as your regex suggests?

(Oops, I didn't see your reply when I posted this :)


 8:10 pm on Apr 2, 2013 (gmt 0)

{3,5} isn't really an OR comparison on two variables (ok I guess {4,5} could be perceived as either 4 or 5, but in the general sense this isn't really what it means). This specifies how many times the previous pattern can repeat. In the case of \d{3,5} this would match a string of between 3 and 5 digits.


 8:15 pm on Apr 2, 2013 (gmt 0)

Hello Penders, erm, the pattern is a serial number range that starts at 0001 and ends 9999 - unfortunately, when this convention was done, the format was done as 001 to 999 (management decision) accidentally. So my aim is to accept all variants (some even came through as 00001 to 99999).

I could just pad the numbers really when entering the numbers, but that isn't what I have been asked to do.

But since reading all replies and searching google a little, I've come to see what I've done wrong.

But thanks for the follow up, always appreciated :)



 10:37 pm on Apr 2, 2013 (gmt 0)

If you had said 0001 to 9999, I would have replied differently. Glad you used Google ...


 12:18 am on Apr 3, 2013 (gmt 0)

Independent answer, looking only at the RegEx aspect


Regex pattern:-


And this doesn't result in a syntax error and/or php going into a coma?

always works.
{3} = exactly three occurrences of preceding element
{3,5} = from 3 through 5

Some RegEx dialects also support
where the first form means "this many or more" and the second form means... well, you're better off not using it, because it may mean either "0 through this many" OR "1 through this many" depending on dialect.

But if the beginning of the string is always exactly "ASH" then you don't need any of this stuff. Just give the literal string.


Not enough information. From these examples alone, I'm guessing
assuming no relationship between the overall length of the string, the number of zeros, and presence or absence of 3 and 5.

Are the numbers always in sequential order? Are any of 3 4 5 mandatory? Is there always at least one zero? Can the numbers after zero be anywhere else in the [1-9] range?

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved