I used RegExp, Now i have problems... Help?
-
So i used regexp to handle my dice roller as part of @sockbot.
and it works actually really well.
you may have noticed him around the place rolling dice for people. and that's awesome.
I'm having a wee bit of a problem though. This is the regexp that i'm using to parse for dice roll requests:
/(^|\s)(?<num>-?\d+)?(d(?<sides>-?\d+)|(d?(?<method>WW|Wolf|F|Fudge|Fate)))(t(?<target>\d+))?(b(?<bonus>-?\d+))?(?<options>[pfr]+)?($|\s)/ig
and that works, but it means that the
target
andbonus
matching groups are locked in that order, one can't specify the bonus before the target (which only has meaning for white wolf rolling right now)how do make this regex handle both orders for
target
andbonus
?
-
Rolling 1d10 Target: 8: 3 Successes: 0
-
I think the only way to do this with a regex is to specify the alternate order explicitely:
((t(?<target>\d+))?(b(?<bonus>-?\d+))?|b(?<bonus>-?\d+)t(?<target>\d+))
(you should probably use non capturing groups for some of the groups defined here...)
It would probably be easier to use
(([tb])(?<param>-?\d+))*
and do the target/bonus validation from the JS code.
-
will need to look into non-capturing groups. i didn't use those because i was positive that JS didn't support those. guess i was wrong.
I'd like to keep the whole validation in a single regex if possible/not too inconvenient (mostly because this bot is for TDWTF)
I'll look at the alternating groups. i was hoping to avoid that on the off chance that i wanted to add more options like that...
Thanks!
-
Tokenize that S* :(
-
...this is why EBNF exists...anyone know of a JS implementation of (E)BNF parsing, either as a generator, or as combinators?
-
You had a problem (parse dice roll requests).
You used RegExp.
Now you have two problems (parse dice roll requests, make regexp work).
-
..this is why EBNF exists...anyone know of a JS implementation of (E)BNF parsing, either as a generator, or as combinators?
ooooh. does nodeJS have an EBNF parsing module that will work?
-
Now you have two problems (parse dice roll requests, make regexp work).
yes, i am aware of this.... that's more or less why i started with it. this is a bot for TDWTF.
@sockbot 20d30pr
-
Rolling 20d30: Prerolling 1 times: 11, 23, 26, 6, 24, 18, 17, 18, 5, 19, 27, 12, 6, 22, 11, 16, 13, 19, 1, 14 Sum: 308
-
does nodeJS have an EBNF parsing module that will work?
Answered my own question: such a thing does exist. See Jison and PEG.js, although I have not used them since I do not have a taste for creating WTFy JS things.
-
You had a problem (parse dice roll requests).
You used RegExp.
Now you have two problems (parse dice roll requests, make regexp work).And a bot for TDWTF naturally should contain WTF.
-
Answered my own question: such a thing does exist. See Jison and PEG.js, although I have not used them since I do not have a taste for creating WTFy JS things.
oh this is going to take a while to wrap my brain around. good thing i have a long weekend coming up.
-
yes, i am aware of this.... that's more or less why i started with it. this is a bot for TDWTF.
@sockbot 20d30pr
Then you already have your WTF. You don't have to figure out how to allow different ordering of the commands. When someone can't figure out that the commands have to be entered in a certain order, you reveal the code and say....
That's the joke.
-
-
That's the joke.
it wouldn't be a funny one then. and besides i think we've used that one before.
also i've been self nerd sniped and have to solve this problem now.
-
Is there a particular reason you have to parse this with one regular expression? Sometimes it is easier to use several, one per major case.
-
Is there a particular reason you have to parse this with one regular expression? Sometimes it is easier to use several, one per major case.
because until now i only needed one? also for the WTFs?
i'm open to using multiple regexes if needed. i just need help figuring out what they need to be.
-
Sometimes it is easier to use several, one per major case.
that's what the | is for, right?!I thought vinduv's answer was /thread, and if that wasn't then mine certainly was.
and if you just want it to be wtfy, combine regex tokenizing + switch statement inner regex parsing + forloop like so...
(i love wtfy javascript for some weird reason)
[code]
"20d30 t3 b8".match(/(\d+d\d+)|(t\d+)|(b\d+)/mgi).forEach(
function(a){
switch (a.match(/(b)|(t)|(d)/)[0]){
case "b":
console.log("Bonus: "+a.replace(/b/,""));
break;
case "t":
console.log("Target: "+a.replace(/t/,""));
break;
case "d":
var tmp = a.split("d");
console.log("Num: "+tmp[0]);
console.log("Sides: "+tmp[1]);
break;
}
}
);
[/code]
-
that's what the | is for, right?!
You can use it like that, but as complexity goes up, it helps to really separate things. It decreases efficiency a bit but makes it easier to figure out WTF is going on. (A proper lexer works like this, sometimes with some higher-level state shifting for things like comments and strings, which are pretty awkward to reliably match with regexps otherwise.)
-
target (which only has meaning for white wolf rolling right now)
Not going to include “threshold”s in that?
-
Not going to include “threshold”s in that?
not yet, but if one would care to point me at a PDF if the relevant sourcebook i would be happy to add it.
-
/(^|\s)(?<num>-?\d+)?(d(?<sides>-?\d+)|(d?(?<method>WW|Wolf|F|Fudge|Fate)))(t(?<target>\d+))?(b(?<bonus>-?\d+))?(?<options>[pfr]+)?($|\s)/ig
Aside: if your language supports verbose regular expressions with comments, using them is a good idea. :)
-
This thread is for making WTFs not solving them!
-
yeah, NodeJS support for those is limited at best. because JavaScript i guess.
still having fun making the bot but..... so many WTFs
Current parser is here; lines 11-64
https://github.com/AccaliaDeElementia/SockBot/blob/master/sock_modules/dice.jsI possibly could have made that parser smaller but that normalization code had to go somewhere and the parser seemed like the best place for it
-
Trying to find one. OTH, thresholds are just a number of extra successes required to succeed.
That is, a difficulty of X with a threshold of 2, requires 3 success dice to actually count as a "real success".
-
hmm.... should be simple enough. but what would the option be tagged as?
t
,b
,r
,s
,p
,f
have all been taken and they are case insensitive.
-
hmm.... should be simple enough. but what would the option be tagged as? t, b, r, s, p, f have all been taken and they are case insensitive.
h. Or d, which would make it TRWTF.
-
d is technically available... that would make the syntax 10dWolfT9D3B0RPFS (where RPFS must be together but the order of them and T B D are variable)
thougths?
-
Add punctuation!
-
?
-
Now imagine me trying to do an impression of Walken saying about more cowbell.
-
given how much he appears in GIS i assume you mean him?
-
How can you not know who Walken is? Seriously?
While I can't find a decent copy of the original cowbell skit on YouTubes, here's something that might indicate why he is awesome.
-
because ive never watched much of latenight television, since ever?
pop culture (outside of Babylon 5 or Star Trek never really appealed to me.....
-
Ever watched A View To A Kill?
-
no, should i add it to the list?
-
no, should i add it to the list?
It's definitely one of the better Bond films, even if it is 1) Roger Moore, 2) complete with screaming Bond girl and 3) cheesy as sin but Walken as the bad guy is awesome.
-
bond film?
huh. i don't recall that title. i thought i had seen all the bond films...
it's on the priority list now!
-
The one where they're going to flood Silicon Valley and the fight on top of the Golden Gate Bridge.
-
oh. i have seen it then. don't remember much of it.
time for a re-watch!
-
Ever watched A View To A Kill?
The watch scene from Pulp Fiction is more... iconic, I guess.
That being said, I really need to catch up with Bond...
-
Wait, you've never seen Balls of Fury?
No, that's not a porn. Well, there's probably a porn with that title, but that's not what I'm talking about.
Also, pre-emptive "the PDA thread is way" comment.
-
It is, yes, but it's been so long since I saw Pulp Fiction I should really rewatch it.
-
Wait, you've never seen Balls of Fury?
Or The Country Bears, for that matter.
Christopher Walken's Best Moments in The Country Bears (Re-upload) – [02:18..04:03] 04:03
— thesouz18
-
Or even Click.
-
Or the Fatboy Slim clip
-
So vimeo gets oneboxed?
Additional WTF for our corp firewall blocking youtube be don't bothering with vimeo.
-
The watch scene from Pulp Fiction is more... iconic, I guess.
That's good. I'd also recommend True Romance (includes a classic Brad Pitt performance, too):
(some NSFW language, because Dennis Hopper)
True Romance - The Sicilian Scene in HD – 10:46
— ImBigOnReddit...or Last Man Standing.