
ShadowsPirate Beginner
Joined: 15 Jun 2007 Posts: 17

Posted: Fri May 09, 2008 9:43 pm
Some Regex help assistance 
This is my first shot at a regex, so yeah...
I am using this pattern
Code: 
^There is a total of (%w) {goldsilvercopper} ((,)? ((and)? ((%w) {silvercopper}))? (and (%w) copper))? coins

and I am trying to match
Quote: 
(1)There is a total of one gold, eight silver and two copper coins
(2)There is a total of one gold and two copper coins
(3)There is a total of one gold and eight silver coins
(4)There is a total of three silver and four copper coins
(5)There is a total of three silver coins
(6)There is a total of six copper coins

I am also using this function from Here to make the words into numbers.
Code: 
#var numbers {onetwothreefourfivesixseveneightnineteneleventwelvethirteenfourteenfifteensixteenseventeeneighteennineteen}
#func getNum {%if( %pos( "", %1)>0, %eval( %db( @hnumbers, %left( %1, %pos( "", %1)1))+%ismember( %right( %1, %pos( "", %1)), @numbers)), %if( %iskey( @hnumbers, %1), %db( @hnumbers, %1), "")%if( %ismember( %1, @numbers), %ismember( %1, @numbers)))}
#addkey HNumbers {twenty=20thirty=30forty=40fifty=50sixty=60seventy=70eighty=80ninety=90}

So in my value I have
Quote: 
#if ((@getnum(%1)!=0)) {totaltax=((@totaltax)+(@getnum(%1)))}
#if ((@getnum(%2)!=0)) {totaltax=((@totaltax)+(@getnum(%2)))}
#if ((@getnum(%3)!=0)) {totaltax=((@totaltax)+(@getnum(%3)))}
#show @totaltax

But I get the wonderful line of
Quote: 
(((((()+()))+()))+())

Hopefully someone can see what I am doing, and thank you to anyone who can help. 



Progonoi Magician
Joined: 28 Jan 2007 Posts: 430

Posted: Mon May 12, 2008 8:45 pm Re: Some Regex help assistance 
ShadowsPirate wrote: 
#if ((@getnum(%1)!=0)) {totaltax=((@totaltax)+(@getnum(%1)))}
#if ((@getnum(%2)!=0)) {totaltax=((@totaltax)+(@getnum(%2)))}
#if ((@getnum(%3)!=0)) {totaltax=((@totaltax)+(@getnum(%3)))}

I hope this answer doesn't come too late.
I glanced over your code and I think the trouble sponges from those lines I quoted here.
Correct would be:
Code: 
#if ((@getnum(%1)!=0)) {totaltax=%eval(@totaltax+(@getnum(%1))}

All the remaining #if lines should be changed in the same manner.
Hope this helps.
Prog 



ShadowsPirate Beginner
Joined: 15 Jun 2007 Posts: 17

Posted: Tue May 13, 2008 1:29 am 
So, and I know it probably has a ton of errors the non regex eye such as mine doesn't spot, is my pattern alright then?




Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom

Posted: Tue May 13, 2008 2:14 am 
Well, it seems to be some kind of mutant child of regex and zScript. You've got the %w and the {onetwo} syntax from zScript and the ? meaning "optional" from regex. That and you're using %w for an item that could potentially be more than one word and also contain hyphens (the number). And you've got optional stuff all over the place, including inside other optional stuff you don't really need. So no, it probably won't work. It might be easier to do:
^There is a total of ([\w\ ]+) (goldsilvercopper)(?:, ([\w\ ]+) (silvercopper))?(?: and ([\w\ ]+) (silvercopper))? coins$
However, while all of the patterns match, some ("There is a total of one gold, eight silver and two copper coins") capture incorrectly, giving %3 as "eight silver and two". I'm not sure what can be done about that. 



ShadowsPirate Beginner
Joined: 15 Jun 2007 Posts: 17

Posted: Tue May 13, 2008 1:52 pm 
Oh, I was somehow thinking they could be used together, and there aren't any hyphenated words in any of the examples I had provided....so not sure where you're getting the hyphen from.




Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom

Posted: Tue May 13, 2008 3:44 pm 
Because if there are more than twenty coins, there's going to be a hyphen. There are twentyeight gold coins. The GetNum function you're using assumes that the number will use hyphens.




ShadowsPirate Beginner
Joined: 15 Jun 2007 Posts: 17

Posted: Tue May 13, 2008 9:33 pm 
Well..., I hadn't realized the thing about the hyphens but I do see it in the function now. But, none of them numbers will be above 10. IE10 copper=1 silver, 10 silver=1 gold, and there definitely won't ever be any more then 5 gold at a time. Maybe that helps a bit?




Leyline Wanderer
Joined: 03 Feb 2007 Posts: 64

Posted: Mon May 19, 2008 10:12 pm 
I did this on one mud (before zmud had regex) and since I am still not great at regex, I just made a trigger for each possible combination....
There is a total of (%w) gold coins.
doTax %1 0 0
There is a total of (%w) silver coins.
doTax 0 %1 0
There is a total of (%w) copper coins.
doTax 0 0 %1
There is a total of (%w) gold, (%w) silver and (%w) copper coins.
doTax %1 %2 %3
There is a total of (%w) gold and (%w) silver coins.
doTax %1 %2 0
There is a total of (%w) gold and (%w) copper coins.
doTax %1 0 %2
There is a total of (%w) silver and (%w) copper coins.
doTax 0 %1 %2
Then I would make an alias that accepts 3 params (gp,sp,cp)
alias
doTax
#if ((@getnum(%1)!=0)) {totaltax=%eval(@totaltax+(@getnum(%1))}
#if ((@getnum(%2)!=0)) {totaltax=%eval(@totaltax+(@getnum(%2))}
#if ((@getnum(%3)!=0)) {totaltax=%eval(@totaltax+(@getnum(%3))}
But, how are you calculating the tax for copper vs gold, should we do like...
doTax
#if ((@getnum(%1)!=0)) {totaltax=%eval(@totaltax+(@getnum(%1))}
#if ((@getnum(%2)!=0)) {totaltax=%eval(@totaltax+(@getnum(%2)/10)}
#if ((@getnum(%3)!=0)) {totaltax=%eval(@totaltax+(@getnum(%3)/100)}
(showing tax in gp and decimals for sp/cp)
Of course reverse the math if you want tax in cp, (multiply gp*100, sp*10) 



ShadowsPirate Beginner
Joined: 15 Jun 2007 Posts: 17

Posted: Tue May 20, 2008 12:18 am 
I was kind of hoping to avoid all of the different triggers, which is why I had (badly) attempted to make a regex with optional items.




Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom

Posted: Tue May 20, 2008 3:11 am 
Well, my regex works, you'll just have to be careful about checking the parameters for the phrase "silver and" and splitting it in two if it's present.





