Welcome to WebmasterWorld Guest from 54.162.44.105

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Regex issue

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

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 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
7:29 pm on Apr 2, 2013 (gmt 0)

New User

joined:Jan 29, 2013
posts: 29
votes: 0


^ASH[0*](34|45)$ is not perfect, but gets there
7:41 pm on Apr 2, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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.
7:53 pm on Apr 2, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 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]

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

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 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
10:37 pm on Apr 2, 2013 (gmt 0)

New User

joined:Jan 29, 2013
posts: 29
votes: 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)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13682
votes: 446


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?
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members