Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Complex regex question

Complex regex question

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Tue 08 Jun 2004 08:22 PM (UTC)
Message
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.

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Flannel   USA  (1,230 posts)  Bio
Date Reply #1 on Tue 08 Jun 2004 08:38 PM (UTC)
Message
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.

~Flannel

Messiah of Rose
Eternity's Trials.

Clones are people two.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #2 on Tue 08 Jun 2004 09:51 PM (UTC)
Message
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>", ",")




- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Poromenos   Greece  (1,037 posts)  Bio
Date Reply #3 on Tue 08 Jun 2004 11:03 PM (UTC)
Message
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...

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
Top

Posted by Flannel   USA  (1,230 posts)  Bio
Date Reply #4 on Tue 08 Jun 2004 11:04 PM (UTC)

Amended on Tue 08 Jun 2004 11:06 PM (UTC) by Flannel

Message
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.

~Flannel

Messiah of Rose
Eternity's Trials.

Clones are people two.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #5 on Wed 09 Jun 2004 01:45 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


15,781 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.