homepage Welcome to WebmasterWorld Guest from 54.226.213.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
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
Matthew1980




msg:4560686
 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:-

ASH00045
ASH0034
ASH034

Regex pattern:-

^[ASH]{3}[0-9]{3,4,5}$

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.

Cheers,
MRb

 

jlnaman




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

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

penders




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

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

^[ASH]{3}

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.

[0-9]{3,4,5}$

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...
^ASH\d{3,5}$

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

Matthew1980




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

UPDATE:
======

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.

[EDIT]

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.

Cheers,
MRb

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

penders




msg:4560739
 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 :)

penders




msg:4560742
 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.

Matthew1980




msg:4560746
 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 :)

Cheers,
MRb

jlnaman




msg:4560786
 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 ...

lucy24




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

Independent answer, looking only at the RegEx aspect

ASH00045
ASH0034
ASH034

Regex pattern:-

^[ASH]{3}[0-9]{3,4,5}$


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

{one-number-here}
or
{first-number,second-number}
always works.
{3} = exactly three occurrences of preceding element
{3,5} = from 3 through 5

Some RegEx dialects also support
{first-number,}
and
{,second-number}
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.

ASH00045
ASH0034
ASH034

Not enough information. From these examples alone, I'm guessing
ASH0{1,3}3?45?
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