Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Matching Numbers 2

Status
Not open for further replies.

Skark166

IS-IT--Management
Aug 6, 2002
54
US
Ok, I've been dinking arround with this pattern matchin stuff for a while now, and can't get this to work right.

I need to check 5 numbers in a string to see if the fall within a xxyyy or xxxyy pattern. Where x and y can equal anything from 1 to 6. (or anything at all for that matter).

Scarecrow
 
So Far, I've come up with the following. It works too, but it'd be nice to know of an abreviation for this as it is a long one...

RegExp(/(1{2}|2{2}|3{2}|4{2}|5{2}|6{2})(1{3}|2{3}|3{3}|4{3}|5{3}|6{3})|(1{3}|2{3}|3{3}|4{3}|5{3}|6{3})(1{2}|2{2}|3{2}|4{2}|5{2}|6{2})/)

Scarecrow
 
I usually start with a few valid strings.

Could you give me a few examples of valid strings please so we can do this together?

Gary Haran
==========================
 
sure, Here's a few different examples.

11666 - true match
33344 - true match
22345 - false match


There are 5 numbers added to a string in sequence ranging from 1 to 6.

I want the pattern match to be able to tell that there are indeed 2 numbers that match and there are 3 number that match (sort of a full house senerio).

Scarecrow
 
Just to clarify a pattern 44444 should match ok

Scarecrow
 
Yeah, I'm pretty new to Jscript. I thought it would be good practice to try and re-create yahtzee.

Thank you for your help

I think it's going to take me some time to try and interpet that pattern :)

Scarecrow
 
/^(([1-6])\2([1-6])\3\3|([1-6])\4\4([1-6])\5)$/ig

you have two choices...
([1-6])\2([1-6])\3\3
or
([1-6])\4\4([1-6])\5)


now see the numbers \n?
each of them represents one of the open parenthesis

the largest one represents the whole expresion \1

the second one \2 represents the value found after the second parenthesis ([1-6]) (by the way this means anything between 1 and 6).
So the expresion ([1-6])\2 means give me a number between 1 and 6 and the digit after that has to be equal to what you gave me first.
from there is just a matter of counting the parenthesis and building your logic.

hope this makes things clearer.
 
a star to grtfercho...is this called a forward lookup? or just referencing a parenthesized match?



=========================================================
while (!succeed) try();
-jeff
 
I've seen different names for this parenthesis index, subexpressions, but the one I like the most and seems more technical to me is Backreferences, because you are trying to refer to something already entered in the string.


Thanks for the **'s .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top