|
Alocer Newbie
Joined: 01 Dec 2010 Posts: 7
|
Posted: Tue Dec 07, 2010 12:10 am
Regex and multiple params |
I'm trying to come up with a way to take player information from a 'who' listing and store it in a database. The key to this is the trigger that will split each person's line into variables to store in the database, but part of it is giving me trouble. To start, an example of what a 'who' looks like:
Code: |
[33 Fire War] Draktun the Warrior of the Gauntlet
[51 Cloud War] Jrelou the Legend of the Battlefield
[36 Human A-P] Hildara the Lady of the Apocalypse, Black Servant
[ Felar AVA] [FORTRESS] Nnaeshuk Ni-Bazuk the Disfigured Warlord of the Holy Brigade
[ 5 Arial Inv] (PK) Mgaaldeh the Advanced Spell Student
[51 Human Shf] Aqeul Sh'ken the Grand Master of Changelings
[15 Storm Ran] Naide the Beastmistress
[38 Svirf War] Rhafaer the Battle-Hardened
[42 Human Bar] Benoni the Master Melody Maker
[19 Storm War] Volundr the Armsman
[51 Arial War] Hrothchuk the Legend of the Battlefield
[35 Duerg War] Elgroth the Warrior of the Blade
[48 Dwarf War] Chenaz the Student of the Macalla
[32 D-Elf War] Jediel the Master of Defense, Sheathed Blade
[51 Elf Pal] Zacchaeus the Champion of the Virtues
[33 Felar A-P] Mayeena the Captain of Legion
[13 Human A-P] Seire the Crimson Sword
[51 Fire War] Sekryelenius the Legend of the Battlefield, Elite Imperial Blade
[36 Felar Shf] Farrouk the Master of Metamorphosis
[44 Human Shf] (WANTED) Jalib the Master of Shapeshifting
* 5 Felar War* (PK) Archibald the White Belt |
Inside the brackets is level, race, and class. The * represents your own character and wouldn't need to be tracked, along with players who don't have a level (wizard). The tricky part is after the brackets, because player "tags" can appear before their name. They are '(PK)', '(WANTED)', or '[CLAN]', where CLAN can be a few different ones. Under certain circumstances, it would be possible to have a player listed like:
Code: |
[51 Human War] (PK) (WANTED) [EMPIRE] Joe the Master of the Schmoes |
Then after the "tags" is the player name (some have a last name that wouldn't need to be saved) and their title (which I'm not interested in). What I wanted to store in the database is level, race, class, if they have a (PK) tag, if they have a (WANTED) tag, if they have a [CLAN] tag, and their first name.
Now that the background information is out of the way, one of my questions is if a single Regex pattern can capture each of those possible tags and when it comes across one and store it into a variable (which I'd probably have to use the %1-99 parameters and assign it myself in the script). Or would I need to use three possible patterns for trying to match each of those tags, then continue with capturing the name?
So far I have the RegEx working for the bracketed information, and my attempt so far is:
Code: |
\[(?playerLevel:[\d ]{2}) (?playerRace:[\w -]{5}) (?playerClass:[\w-]{3})\] ([([][A-Z]+[])] )+(\w+).* |
Using the previous example, besides the data inside the brackets, this only captures [EMPIRE] and Joe. Is there a way to have the "tag" pattern captured 0 to 3 times and store them into parameters automatically, or would I need something more like:
Code: |
\[(?playerLevel:[\d ]{2}) (?playerRace:[\w -]{5}) (?playerClass:[\w-]{3})\] ([([][A-Z]+[])] )?([([][A-Z]+[])] )?([([][A-Z]+[])] )?(\w+).* |
I'm still rather new to using Regex besides the most basic of applications, so any changes for efficiency or insight would be more than appreciated. Thank you! |
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Tue Dec 07, 2010 1:15 am |
Code: |
\[(?:\s+)?(\d+)\s([\w- ]+)\s(\w+)\]\s(?:(\(PK\))\s)?(?:(\(WANTED\))\s)?(?:(\[\w+\])\s)?(\w+) |
%1 is always level
%2 is always race
%3 is always class
%4 is always (PK), even if it's not there.
%5 is always (WANTED), even if it's not there.
%6 is always [CLAN], even if it's not there.
%7 is always player name.
Hope this helps.
Edit: I've never used the tag patterns, so not sure, but the above should help you out a bit. |
|
|
|
Alocer Newbie
Joined: 01 Dec 2010 Posts: 7
|
Posted: Tue Dec 07, 2010 3:01 am |
I just had to move the hyphen in the first word match, but it seems to work great so far. Thank you!
|
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Tue Dec 07, 2010 3:10 am |
Put the hyphen in there, but after the space: [\w -] instead. Otherwise, it won't capture D-Elf. Glad it works out for you.
|
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|