Complex regex question

Posted by Poromenos on Tue 08 Jun 2004 08:22 PM — 6 posts, 21,406 views.

Greece #0
The MUD I play on sends this:
You have 102,123,543,534 gold coins.
Is there any way to make a regex that returns the number of coins regardless of commas? So far I have this:
((\d+)\,|)((\d+)\,|)(\d+)
and i return %2%4%5 but I was wondering if there was a better way that matches on more commas/whatever.
USA #1
This is assuming you want to get rid of the commas in your return? Otherwise you could simply include the commas in the regexp ([0-9\,]+). Or group your numbers into threes, or whatnot.
Australia Forum Administrator #2
I don't think you will make a regexp that will automatically make each one of an indefinitely long list assign to different wildcard numbers. So, I would do something simple and then Split it.

eg.

(?P<coins>((\d{1,4},)*)(\d{1,4}))

This would match any number of repetitions (zero or more with the commas, followed by the last block) and save as wildcard "coins".

Then you could do:

x = Split ("%<coins>", ",")



Greece #3
Right, Flannel, I want to get rid of the commas. And Nick, I don't want to use scripting if possible, that's why I'm looking for the regex. As you said, it would assign each one to a different number, so I assume it's not possible? I guess I'll keep using this one, it's not as if anyone has anything bigger than billions anyway...
USA #4
Nick, why you using \d{1,4}? Why not 1,3? You shouldnt have a grouping of more than three, right?

And Poromenos, I dont think youll be able to get rid of the commas, and match any number, in one regexp. Because for each set, you'd have a different wildcard for it.

Unless you could use forward looking things, since they dont match, to maybe look, but not match, the commas, but still, I dont see how that would work.
Amended on Tue 08 Jun 2004 11:06 PM by Flannel
Australia Forum Administrator #5
Quote:

Nick, why you using \d{1,4}? Why not 1,3?


Sorry, I misunderstood the original question. I thought he was talking about piles of coins, not one very large pile.

In that case, you really want a regexp that matches like mine did, but use scripting to remove the commas, eg.

x = CInt ( world.Replace ("%<coins>", ",", ""))

That takes the number, strips the commas and converts to an integer (probably an integer would be too small, but that is the general idea).

However doing it without scripting?

Probably your original idea would work, after all how many coins are you really expecting? About 6 groups (3 digits each) is likely to be the most the MUD could possibly output.