reg expression matching

Posted by Neverwhere on Thu 10 Jul 2003 04:45 AM — 8 posts, 27,466 views.

USA #0
Ok, I like to think that Im farely skilled with reg exp by now, but I still cant get this... On the MUD I play there is something that shows the amount of exp gained over the total time youve played

|Exp gained | 0 |

however, this can change, but the format always is the same width

Ex:
|Exp gained | 0 |
|Exp gained | 10235320 |

I would like a trigger to match that line, but have a reg exp to gather the amount of xp total. I cant get it to match because it always changes in the amount of spaces before the second | and the digits... ive tried using * but keep getting 'Nothing to repeat at offset'
Any ideas?
Australia Forum Administrator #1
I would do something like this:


^\| Exp gained \|\s+(\d+)\s+\|$


In regexps saying (something)+ means "one or more of something", and using \s means "a whitespace character", so putting it together, \s+ means one or more spaces.

Similarly \d means a digit (0 - 9).
Amended on Mon 14 Jul 2003 08:21 AM by Nick Gammon
USA #2
ok, but when i put the \d+ part into () it doesnt work anymore...

|\s+(\d+)\s+| <-- Doesnt match anything... should I put the \s+ 's into () also?
USA #3
Do you need the wildcard to put into a variable?

The \d+ doesn't save the number to a variable, so you'd have to do:

^\| Exp gained \|\s+(.*)\s+\|$

I think. It's been a while since I've had to use regular expressions like that.
Greece #4
Everything returns if you enclose it in parentheses... Maybe you have to escape the pipes (|) though?
Australia Forum Administrator #5
Quote:

The \d+ doesn't save the number to a variable ...


It will with the brackets around it, that is why I had them.

You need to escape the "|" symbols, as I did in my earlier post, otherwise they have the meaning "this" OR "that".
USA #6
Quote:

^\| Exp gained \|\s+\d+\s+\|$


I dont see the brackets that you said to put there, so thats why I inquired as to why it wasnt returning anything and what should be put into brackets
Amended on Mon 14 Jul 2003 08:01 AM by Neverwhere
Australia Forum Administrator #7
Hmmm - my brain must have malfunctioned there. :)

I have amended the post to put the brackets in.

However looking at your original post a bit more closely, I see you have more spaces than are obvious between the vertical bars. So it should probably read:


^\|\s*Exp gained\s*\|\s+(\d+)\s+\|$


The extra \s are to allow for the spaces on each side of "Exp gained" - by following by "*" it means zero or more, which would allow for the "Exp" being hard up against the bar.